DEV Community

Dipesh Gunwant
Dipesh Gunwant

Posted on

Error in Javascript

Hey everyone, I was creating a simple program to give me output (sum of every digit of the value given by user) but the problem is, whenever user is giving a value more than 10 digits so the program gives wrong output. Here's the program:-

let n=566;
s=0;
while(n>9){
while(n>0){
r=n%10;
n=n/10>>0;
s+=r;
}
if(s<10){
console.log(s);
}else{
n=s;
s=0;
}
}

Input : 566
Output : 5+6+6= 17....1+7= 8
8 is final output

Input : 987654324567
Output : 9+8+7+6+5+4+3+2+4+5+6+6= 66....6+6= 12....1+2= 3
3 should be final answer but it's giving me 7

Top comments (0)