What is the growth? ( 6 kyu )

문제

Write the function getGrowth(), which accepts an arbitrary number of comma separated integers and returns the difference between each subsequent and previous number in the series, as a string.

The function must return a string value where each “growth instance” is separated by a comma followed by a space character.

Example 1:

getGrowth(1, 2, 3)  // returns the string: "1 (+0, +0%), 2 (+1, +100%), 3 (+1, +50%)"

배열이 들어오면 이전값과 비교해서 변화를 적어 리턴하는 문제

풀이

function getGrowth(...args){
  let result = `${args[0]} (+0, +0%), `;
  for ( let i = 1; i < args.length; i++ ) {
    if ( args[i]-args[i-1] >= 0 ) {
      result += `${args[i]} (+${args[i]-args[i-1]}, +${Math.round((args[i]-args[i-1])/args[i-1]*100)}%), `
    } else {
      result += `${args[i]} (${args[i]-args[i-1]}, ${Math.round((args[i]-args[i-1])/args[i-1]*100)}%), `
    }
  }
  return result.slice(0, result.length-2);
}

그냥 써져있는대로 맞춰 씀..

다른 사람의 풀이

비슷함


'자료구조, 알고리즘 > Codewars 문제' 카테고리의 다른 글

Array Deep Count ( 6 kyu )  (0) 2017.12.27
Fibonacci, Tribonacci and friends ( 6 kyu )  (0) 2017.12.23
What is the growth? ( 6 kyu )  (0) 2017.12.22
Dashatize it ( 6 kyu )  (0) 2017.12.20
String average ( 6 kyu )  (0) 2017.12.19
Array Helper ( 6 kyu )  (0) 2017.12.19

+ Recent posts