function should return didginal root of number, this function return 'undefined' always, but variable contain corectly result ? Where is mistake?
function nuberRoot(a:number):number{
let s:number = 0;
while(a>0){
s =a
a = Math.floor(a /10)
}
if (s>=10){
nuberRoot(s)
}
else{
alert(s)//s=6
return s//undefined
}
}
alert(nuberRoot(942))//undefined
CodePudding user response:
You have missing return for if(s>=10)
function nuberRoot(a){
let s = 0;
while(a>0){
s =a
a = Math.floor(a /10)
}
if (s>=10){
return nuberRoot(s)
}
else{
alert(s)//s=6
return s//undefined
}
}
alert(nuberRoot(942))//undefined
CodePudding user response:
Logic is correct but return when s>10. Thanks.
if (s>=10){
return nuberRoot(s);
}