Im attempting to write this function that returns the sum of all the digits of a given Number
i keep getting "sum is not defined" error and suspect scope to be the issue...
function sumOfDigits(num) {
// Write your code here!
let numArr = num,
output = [],
sNumber = num.toString();
for (let i = 0, len = sNumber.length; i < len; i++) {
output.push(+sNumber.charAt(i));
}
for (let x = 0, sum = 0; x < output.length; sum += output[x++]);
return sum;
}
I'm guessing its a scope issue since i believe that i have defined "sum" to my knowledge