Home > OS >  Error in true false output in Array Problem
Error in true false output in Array Problem

Time:07-08

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?

CodePudding user response:

  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));

CodePudding user response:

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
}

CodePudding user response:

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));

  • Related