I wrote a code, it works normally in my editor, but when i upload it to codewars, i saw a error code TypeError: n.split is not a function
I tried to add "n.toString()"(i saw it in other question) but it doesn't work. What i did wrong?
"use strict"
let sum = 0;
function digital_root(n) {
n.toString();
let arr =
(n).split("").map(Number);
for(let i=0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
console.log(digital_root("16"));
You need to actually set n to it's string representation.
let sum = 0;
function digital_root(n) {
n = n.toString();
let arr = n.split("").map(Number);
for(let i=0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
console.log(digital_root("16"));