Why doesn't this while loop close?

const n = 1234;
const typecast = (n) => Number(n);

function sum_reducer(accumulator, currentValue) {
  return accumulator + currentValue;

function stringToArray(string) {
  const arrayFromString = Array.from(String(string), typecast);
  return arrayFromString;

function digitalRoot(n) {
  const nArr = stringToArray(n);
  const reducedStr = nArr.reduce(sum_reducer);
  const reducedStrLength = reducedStr.toString().length;

  do {
    const reducedArray = stringToArray(reducedStr);
    const evenMoreReducedArr = reducedArray.reduce(sum_reducer);
    const reducedStrLength = evenMoreReducedArr.toString().length;
  } while (reducedStrLength > 1);
  console.log(`Final ${reducedStrLength}`);
  return reducedStrLength;


I don’t understand the questions. By “close” you mean “end”? It won’t end until it is false, until reducedStrLength > 1 is false. That is (apparently) never false.

One thing to notice is that you have shadowing variables, two variables with the same name in two different scopes - “reducedStrLength” is defined twice.

What is it you are trying to do here?

Yeah, just declaring that variable once gets it to “work”, but I’m still not clear what the purpose is. If you keep reducing, won’t the final length always be 1?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.