Map function - adding arrays as integers, not as strings

Hi for the map function, it is adding the strings onto each other rather than adding them as numbers (0161 rather than 0+1+6+1). what can i do to change this? thanks

function digital_root(n) {

  //create a marker to track the length of the output

  let nLength = n.toString().length;

  //if its greater than >1 continue the loop

  //create a store for the new n if its >1

  let summation = 161;

  let currentAnswer = 0;

  //loop

  while (nLength > 1) {

    summation

      .toString()

      .split("")

      .map(number => parseInt((currentAnswer = currentAnswer + number)))

      .join();

    nLength = summation.length;

  }

  return summation;

  // initially split the digits into its parts and add them together

  //if the length of the new n is > 1, continue the loop/

  //otherwise break out of the loop

}

digital_root(161);

.map() is a no-go here. Take a look at .reduce():

1 Like

yep i realised. but still returning string values not integer. i’ll look further into this. (reducer - array into integer haha always forget)

function digital_root(n) {

//create a marker to track the length of the output

let nLength = n.toString().length;

//if its greater than >1 continue the loop

//create a store for the new n if its >1

let summation = 161;

let currentAnswer = 0;

//loop

// while (nLength > 1) {

// summation

// .toString()

// .split("")

// .map(number => parseInt(+(currentAnswer = currentAnswer + number)))

// .join();

// nLength = summation.length;

// }

const reducer = (currentAnswer, currentValue) => currentAnswer + currentValue;

while (nLength > 1) {

summation

  .toString()

  .split("")

  .reduce(reducer)

nLength = summation.length;

}

return summation;

// initially split the digits into its parts and add them together

//if the length of the new n is > 1, continue the loop/

//otherwise break out of the loop

}

digital_root(161);

split doesn’t automatically covert to numbers. You convert to string to split them but then you never convert back to numbers, so that’s how strings behave with the + concat operator

1 Like

You might wanna see this first anyway :wink:

Got it - for future reference put a + sign on the string to coerce it to an integer

wonder if ParseInt would also do the same thing

function digital_root(n) {
  //create a marker to track the length of the output
  let nLength = n.toString().length;
  //if its greater than >1 continue the loop

  //create a store for the new n if its >1

  let summation = 0;

  while (nLength > 1) {
    n = n
      .toString()
      .split("")
      .reduce((sum, digit) => sum + +digit, 0);
    nLength = n.toString().length;
  }

  return n;
  // initially split the digits into its parts and add them together
  //if the length of the new n is > 1, continue the loop/
  //otherwise break out of the loop
}

digital_root(456);

Number.parseInt(digit) can convert your digit character to a number. But I usually use Number(digit) for short.

1 Like