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

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

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