Please break this down | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please break this down

// I am self teaching js now I’m with the loops and came across this example, I have no idea how the code outputs 55 can someone please break it down in simple English. To me I counted output to be 11 let count =1; let total =0; while (count <= 10){ total = total + count; count = count + 1; } document.write(total);

20th May 2021, 6:03 AM
Mawande Notununu
Mawande Notununu - avatar
5 Answers
+ 2
This is now cristal clear 👌🏿
20th May 2021, 7:07 AM
Mawande Notununu
Mawande Notununu - avatar
+ 1
You are a star thank you very much
20th May 2021, 6:49 AM
Mawande Notununu
Mawande Notununu - avatar
0
👋👋 This code can be written shorter: //count is 1 let count = 1; //the total is 0 let total = 0; //loop finishes when count reaches 10 while(count <= 10) {     //nests count value in total, and adds them     total += count;     //increase count value one by one     count++; } //prints total (the sum of printed counts document.write(total); //1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 /* output: 55 */ You increase count value one by one, and add it to total variable. So this happens when you write total += count: total = 0; now total is 0 Loop starts total = 1 + 0 = 1; now total is 1 total = 2 + 1 = 3; now total is 3 total = 3 + 3 = 6; now total is 6 total = 4 + 6 = 10; now total is 10 total = 5 + 10 = 15; now total is 15 total = 6 + 15 = 21; now total is 21 total = 7 + 21 = 28; now total is 28 total = 8 + 28 = 36; now total is 36 total = 9 + 36 = 45; now total is 45 total = 10 + 45 = ; now total is 55 Loop finished Printing total = printing 55 If you needed more informations, contact me on Sololearn.
20th May 2021, 6:57 AM
🇮🇱 Radin Masiha 🇮🇱
🇮🇱 Radin Masiha 🇮🇱 - avatar
0
Happy coding 👋
20th May 2021, 6:57 AM
🇮🇱 Radin Masiha 🇮🇱
🇮🇱 Radin Masiha 🇮🇱 - avatar
- 1
https://code.sololearn.com/Wv3lJnu2B13Q/?ref=app I believe it fills ur need to understand each iterations ^^
20th May 2021, 6:40 AM
Nivya
Nivya - avatar