This question already has an answer here:
function add(n){
var arrtemp = n.toString().split('').map((e)=> parseInt(e))
var temp = arrtemp.reduce((a,b)=> a+b);
var arrtemp2 = temp.toString().split('').map((e)=> parseInt(e))
if(arrtemp2.length>1 ){
add(temp);
}
else {
return (temp);
}
}
I am trying to add the digits of a number and if the sum is 2 digit then it will again add the digits till the result is single digit. But the else statement is not returning the value.
Aucun commentaire:
Enregistrer un commentaire