-2

Why is 'i' not increased? What's the problem? It outputs '0' in console

function digitalRoot(n) {
  i = 0;
  arr = [];
  let length = n.length;
  while (i < length) {
    arr.push(n[i]);
    i++;
  }
  console.log(i); // output: 0
}

digitalRoot(11);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 8
    why are you using n.length if n is a number already? and iniciate all your variables with let , var or const – Anon Feb 17 '23 at 20:04
  • I need to count its length. I mean "234" = length: 3; "11" = length: 2 and others. I've iniciated it in VS code and nothing changed – AlexanderRise Feb 17 '23 at 20:06
  • @Anon from the declaration 2 lines down, it seems n is of type Array, not an int – Kebab Programmer Feb 17 '23 at 20:06
  • 5
    if you want to passdown a string then use `digitalRoot("11");` rigth now you are just passing a number – Anon Feb 17 '23 at 20:06
  • What is this code supposed to do? – Konrad Feb 17 '23 at 20:06
  • 4
    @KebabProgrammer The call to the function at the very bottom confirms it is an int. They're just trying to use it as either a string or an array (same thing really, a string is an array of characters under the hood). – Jesse Feb 17 '23 at 20:07
  • 3
    The while loop never executes, because 0 is not smaller than undefined. – code Feb 17 '23 at 20:07
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Feb 17 '23 at 20:08

3 Answers3

2

Here is a digitalRoot calculation

It will loop until the sum of the digits in the input has the length 1

The digital root (also repeated digital sum) of a natural number in a given radix is the (single digit) value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. For example, in base 10, the digital root of the number 12345 is 6 because the sum of the digits in the number is 1 + 2 + 3 + 4 + 5 = 15, then the addition process is repeated again for the resulting number 15, so that the sum of 1 + 5 equals 6, which is the digital root of that number. Wikipedia

const digitalRoot = num => {
  let len = num.toString().length;
  while (len > 1) {
    num = [...num.toString()].reduce((a,b) => (+a) + (+b));
    len = num.toString().length;
  }
  return num
};  

console.log(digitalRoot(11)); // 2
console.log(digitalRoot(1999)); // 1

In base 10, this is equivalent to taking the remainder upon division by 9 (except when the digital root is 9, where the remainder upon division by 9 will be 0), which allows it to be used as a divisibility rule.

const digitalRoot = num => {
  if (num === 0) return 0;
  if (num % 9 === 0) return 9;
  return num % 9;
};

console.log(digitalRoot(11)); // 2
console.log(digitalRoot(1999)); // 1

If you want the multiplicative digitalRoot you can do this

const multiplicativeDigitalRoot = num => {
  let persistence = 0;
  while (num > 9) {
    num = [...num.toString()].reduce((a, b) => a * b);
    console.log(num)
    persistence++;
  }
  return { root: num, persistence }; // second number is how many iterations it took
};

console.log(multiplicativeDigitalRoot(11)); // 1,1
console.log(multiplicativeDigitalRoot(1999)); // 2,4
console.log(multiplicativeDigitalRoot(277777788888899)); // 0, 11
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

I guess that you want to have an array of digits:

function digitalRoot(n) {
  const str = n.toString()
  i = 0;
  arr = [];
  while (i < str.length) {
    arr.push(str[i]);
    i++;
  }
  console.log(i);
  console.log(arr)
}

digitalRoot(11);

Shorter:

function digitalRoot(n) {
  console.log([...n.toString()])
}

digitalRoot(11);
Konrad
  • 21,590
  • 4
  • 28
  • 64
-2

Since you're passing a number in "digitalRoot(11);", the "n.length" will be undefined and the While is never triggered.

If you pass down an array instead, it will work just fine!

Felix
  • 1