As everyone is refferring – weight is the sum of the numbers, not digits.
And the function is simple:
var w = 0;
for (var i=0; i<arr.length; i++) {
w = w + arr[i];
}
console.log(w);
But if you want to add together all the digits, you were almost correct – number to string, split it, back to integer and sum it up.
var w = 0;
for (var i=0; i<arr.length; i++) {
x = arr[i].toString();
for (var j=0; j<x.length; j++){
ss = ss + parseInt(x.charAt(j)); //add digit by digit
}
w = w + ss; //add the sum of digits of 1 number to the total sum
}
console.log(w);
2
solved What is the “weight” of an array? [closed]