-2

Here's the question:

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

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

My Code is:

function narcissistic(value) {
  let vLen = value.length;
  let sum = 0;
  for (let i = 0; i < vLen; i++) {
    sum += Math.pow(value[i], vLen);
  }
  if (sum == value) {
    return true;
  } else {
    return false;
  }
}

But I'm getting errors. What should I do?

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
  • 1
    "But I'm getting errors. What should I do?" Show your errors, make a [mre]! – 0stone0 Jul 07 '22 at 14:38
  • I've edited your question to add a Stack Snippet. If you add examples of how you call the function, we can see what errors you are getting. You can edit the snippet and add lines like `console.log(narcissistic('153'))` (or however you're calling it) to the end. – Heretic Monkey Jul 07 '22 at 14:41
  • Does this answer your question? [Get number of digits with JavaScript](https://stackoverflow.com/questions/14879691/get-number-of-digits-with-javascript) – 0stone0 Jul 07 '22 at 14:42
  • describe( "Narcissistic Function", function() { it( "should find small numbers are all narcissistic", function() { Test.assertEquals(narcissistic( 7 ), true, "7 is narcissistic" ); }); it( "should find these numbers are narcissistic", function() { Test.assertEquals(narcissistic( 371 ), true, "371 is narcissistic" ); }); }); – Tirtharaj Roy Jul 07 '22 at 14:46

3 Answers3

2
  1. Numbers don't have .length, convert to string first
  2. vLen[i], you cant treat a number as array, again, convert to string to use that syntax.
  3. The return can be simplefied to return (sum === value);

function narcissistic(value) {
  let sVal = value.toString();
  let vLen = sVal.length;
  
  let sum = 0;
  for (let i = 0; i < vLen; i++) {
    sum += Math.pow(sVal[i], vLen);
  }
  
  return (sum === value);
}

console.log(narcissistic(153));
console.log(narcissistic(111));
0stone0
  • 34,288
  • 4
  • 39
  • 64
0

Well... There are several things wrong with this code, but I think there is mostly a problem with the types of your input.

I'll show you how you can cast the types of your input to make sure you work with the types you need:

Also... You should try to avoid using the == operator and try to use === instead (same goes for != and !==), because the == and != don't try to match the types, resulting in sometimes unpredictable results

function narcissistic(value) {
  valueStr = String(value);
  let vLen = valueStr.length;
  let sum = 0;
  for (let i = 0; i < vLen; i++) {
    sum += Number(valueStr[i]) ** vLen;
  }
  if (sum === value) {
    return true;
  } else {
    return false;
  }
}

if(narcissistic(153)) {
  console.log("narcissistic(153) is true!") // expected value: true
}
Figares
  • 63
  • 6
-2

All the first 9 digits from 1 to 9 is Narcissistic number as there length is 1 and there addition is always same.

  • So, first we are checking weather the number is greater than 9 or not.

  • if(num>9) =>false than it's a narcissistic number. -if(num>9) =>true than we have to split number into digits for that I have used x = num.toString().split('');. Which is first converting number to String and than using split() function to split it.

  • Than , we are looping through each digit and digitsSum += Math.pow(Number(digit), x.length); adding the power of digit to const isNarcissistic = (num) => { let x = 0; let digitsSum.

  • at the end, we are comparing both num & digitsSum if there are matched than number is narcissistic else not.

    const isNarcissistic = (num) => {
      let x = 0;
      let digitsSum = 0;
      if (num > 9) {
        x = num.toString().split('');
        x.forEach(digit => {
          digitsSum += Math.pow(Number(digit), x.length);
        });
        if (digitsSum == num) {
          return true;
        } else {
          return false;
        }
      } else {
        return true;
      }
    }
    console.log(isNarcissistic(153));
    console.log(isNarcissistic(1634));
    console.log(isNarcissistic(1433));
    console.log(isNarcissistic(342));
Nexo
  • 2,125
  • 2
  • 10
  • 20
  • 2
    Your code would be more helpful (to OP and future readers) if you included a explanation. Maybe some comments and a summary of the approach. – mousetail Jul 07 '22 at 14:42
  • 2
    If you give an answer, explain what you did, don't just dump code. – Gert B. Jul 07 '22 at 14:43
  • Stack Overflow is not a programming contest site. We're not impressed by how you solved the Kata. Answers need to answer the question, which in this case is asking how to fix their code, not write new code. – Heretic Monkey Jul 07 '22 at 14:45
  • I have added explanation. – Nexo Jul 07 '22 at 14:58