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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay