[알고리즘] 2024-02-05(월)

dev-riverkim·2024년 3월 13일
0

8 kyu - Are You Playing Banjo?

Create a function which answers the question "Are you playing banjo?".

If your name starts with the letter "R" or lower case "r", you are playing banjo!

The function takes a name as its only argument, and returns one of the following strings:

name + " plays banjo"
name + " does not play banjo"

Names given are always valid strings.

Solution

function areYouPlayingBanjo(name) {
  return name[0] === 'R' || name[0] === 'r' ? `${name} plays banjo` : `${name} does not play banjo` ;
}
function areYouPlayingBanjo(name) {
  return name + (name[0].toLowerCase() == 'r' ? ' plays' : ' does not play') + " banjo";
}
function areYouPlayingBanjo(name) {
  if (name[0].toLowerCase() === 'r') {
    return name + ' plays banjo';
  } else {
    return name + ' does not play banjo';
  }
}

8 kyu - Third Angle of a Triangle

You are given two interior angles (in degrees) of a triangle.

Write a function to return the 3rd.

Note: only positive integers will be tested.

https://en.wikipedia.org/wiki/Triangle

Solution

function otherAngle(a, b) {
  return 180 - (a + b);
}
const otherAngle = (a, b) => 180 - a - b

8 kyu - Fake Binary

Given a string of digits, you should replace any digit below 5 with '0' and any
digit 5 and above with '1'. Return the resulting string.

Note: input will never be an empty string

Solution

function fakeBin(x){
//숫자 문자열이 주어지면 5 이하의 숫자는 '0'으로, 5 이상의 숫자는 '1'로 바꿔야 합니다. 결과 문자열을 반환합니다.
//참고: 입력은 절대 빈 문자열이 될 수 없습니다.
  
  const str = x.split("");
  const result = str.map((i)=>{
    return Number(i) >= 5 ? 1:0
  });
  
return result.join("")
}
function fakeBin(x) {
    return x.split('').map(n => n < 5 ? 0 : 1).join('');
}
function fakeBin(x) {
  return x.replace(/\d/g, d => d < 5 ? 0 : 1);
}

8 kyu - Grasshopper - Personalized Message

Create a function that gives a personalized greeting. This function takes two parameters: name and owner.

Use conditionals to return the proper message:

casereturn
name equals owner'Hello boss'
otherwise'Hello guest'

Solution

function greet (name, owner) {
  return `Hello ${name === owner ? 'boss' : 'guest'}` 
}
function greet (name, owner) {
  return name === owner ? 'Hello boss' : 	'Hello guest';
}

8 kyu Grasshopper - Terminal game move function

Terminal game move function

In this game, the hero moves from left to right. The player rolls the dice and moves the number of spaces indicated by the dice two times.

Create a function for the terminal game that takes the current
position of the hero and the roll (1-6) and return the new position.

Example:

move(3, 6) should equal 15

Solution

function move (position, roll) {
  // return the new position
  return position + (roll*2)
}
const move = (position, roll) => position + roll * 2
function move (position, roll) {
  return position + roll * 2
}

8 kyu - Grasshopper - Check for factor

This function should test if the factor is a factor of base.

Return true if it is a factor or false if it is not.

About factors

Factors are numbers you can multiply together to get another number.

2 and 3 are factors of 6 because: 2 * 3 = 6

  • You can find a factor by dividing numbers. If the remainder is 0 then the number is a factor.
  • You can use the mod operator (%) in most languages to check for a remainder

For example 2 is not a factor of 7 because: 7 % 2 = 1

Note: base is a non-negative number, factor is a positive number.

Solution

function checkForFactor (base, factor) {
  return base % factor === 0 ? true : false
}
function checkForFactor (base, factor) {
  return base % factor === 0;
}
const checkForFactor = (base, factor) => base % factor === 0;
profile
dev-riverkim

0개의 댓글