Find the divisors! ( 6 kyu )

문제

Create a function named divisors/Divisors that takes an integer and returns an array with all of the integer’s divisors(except for 1 and the number itself). If the number is prime return the string ‘(integer) is prime’ (null in C#) (use Either String a in Haskell and Result, String> in Rust).

Example:

divisors(12); // should return [2,3,4,6]
divisors(25); // should return [5]
divisors(13); // should return "13 is prime"

You can assume that you will only get positive integers as inputs.

약수 찾기

풀이

function divisors(integer) {
  let result = [];
  for ( let i = 2; i < integer; i++ ) {
    if ( Number.isInteger(integer/i) ) {
      result.push(i)
    }
  }
  return result.length !== 0 ? result : `${integer} is prime`;
};

설명할게 없다.

다른 좋은방법이 있을것 같은 문제

다른 사람의 풀이

없네..


+ Recent posts