알고리즘 14 - Does my number look big in this?

박진현·2021년 7월 14일
0

Q.

A Narcissistic Number is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10).

For example, take 153 (3 digits), which is narcisstic:

1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

and 1652 (4 digits), which isn't:

1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938

The Challenge:

Your code must return true or false depending upon whether the given number is a Narcissistic number in base 10.

Error checking for text strings or other invalid inputs is not required, only valid positive non-zero integers will be passed into the function.

A)

function narcissistic(value) {
  // Code me to return true or false
  let str = String(value)
  let result = 0
 
  for(i=1;i<10;i++) {
    if (result === value) {
      return true
    }
    result = 0
    for(j=0;j<str.length;j++) {
      result += (+str[j])**i
      }
    }
  return false
}
profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글