I was certain i had my code right, however it returns 1364 rather than the correct answer 1366. I tested my code for 2^15 and it returned the correct answer 26. I also tried a few other numbers, all of which gave me a correct answer. I know my code is very elementary, i don't know much Javascript and i am open to alternate solutions, however i am looking for an explanation as to why my code does not work.
//adds digits of this number
sum(Math.pow(2,1000));
function sum(x) {
//the upper limit of the number
var c = Math.floor(Math.log10(x));
//the sum
var s = 0;
//the ith digit
var a;
for (var i = c; i >= 0; i--) {
//this works, test by removing the slashes before console.log(a)
a = Math.floor((x % Math.pow(10,i+1)) / Math.pow(10,i));
s += a;
//console.log(a);
}
console.log(s);
}