var value = 2568,
sum = value
.toString()
.split('')
.map(Number)
.reduce(function (a, b) {
return a + b;
}, 0);
document.querySelector('p').innerHTML= sum;
alert(sum);
Total sum Total sum to 1 digit
var value=2568;
function sum(value){
var sum=value.toString().split("")
.reduce(function(a,b) {
return a+parseInt(b);
},0);
return sum;
}
while(value > 9){
value=sum(value);
}
No comments:
Post a Comment