[알고리즘] 2024-02-23(금)

dev-riverkim·2024년 3월 13일
0

8 kyu - Multiply

This code does not execute properly. Try to figure out why.

Solution

function multiply(a, b){
  return a * b
}

8 kyu - Convert a Number to a String!

We need a function that can transform a number (integer) into a string.

What ways of achieving this do you know?

Examples (input --> output):

123  --> "123"
999  --> "999"
-100 --> "-100"

Solution

function numberToString(num) {
  return `${num}`
}
function numberToString(num) {
  return num.toString();
}
function numberToString(num) {
  // Return a string of the number here!
  return String(num);
}
function numberToString(num) {
  return ''+num;
}

8 kyu - Grasshopper - Debug sayHello

Debugging sayHello function

The starship Enterprise has run into some problem when creating a
program to greet everyone as they come aboard. It is your job to fix the
code and get the program working again!

Example output:

Hello, Mr. Spock

Solution

function sayHello(name) {
  return `Hello, ${name}`
}
const sayHello = name => `Hello, ${name}`;

7 kyu - Is this a triangle?

Implement a function that accepts 3 integer values a, b, c. The function should
return true if a triangle can be built with the sides of given length
and false in any other case.

(In this case, all triangles must have surface greater than 0 to be accepted).

Examples:

Input -> Output
1,2,2 -> true
4,2,3 -> true
2,2,2 -> true
1,2,3 -> false
-5,1,3 -> false
0,2,3 -> false
1,2,9 -> false

Solution

function isTriangle(a,b,c)
{
  // 삼각형 성립조건
  // 가장 긴 변의 길이가 나머지 두 변의 길이의 합보다 작아야 함
  
  // 가장 큰 값을 구하고 나머지 두 값을 더해서 비교
  
  const arr = [a,b,c];
  
  let value = 0;
  
  arr.map((item) => {
    return item > value ? value = item : value
  })
  

  return value +
  
  
}
function isTriangle(a, b, c) {
  return (a + b > c) && (a + c > b) && (b + c > a);
}
function isTriangle(a, b, c) {
  let arr = [a, b, c].sort();
  return arr[2] < (arr[0] + arr[1]);
}
function isTriangle(a, b, c) {
  let max = Math.max(a, b, c);
  return max < (a + b + c - max);
}
function isTriangle(a, b, c) {
  let max = Math.max(a, b, c);
  return max < (a + b + c) / 2;
}
profile
dev-riverkim

0개의 댓글