Please what is the explanation of this code, it's a JavaScript program | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please what is the explanation of this code, it's a JavaScript program

<script> var result = 1; var counter =0; while (counter<10){ result=result *2; counter =counter +1; } document.write(result) ; </script>

5th May 2019, 11:48 AM
Williams
Williams - avatar
3 Answers
+ 4
a while loop is running until the condition is false (if counter = 10 -> 10 < 10 -> false). counter is 0 -> 0 < 10 result = 1 * 2 = 2 counter = 0 + 1 = 1 counter = 1 -> 1 < 10 result = 2 *2 = 4 counter = 1 + 1 = 2 -> 2 < 10 result = 8 counter = 3 -> 3 < 10 result = 16 counter = 4 -> 4 < 10 result = 32 counter = 5 -> 5 < 10 result = 64 counter = 6 -> 6 < 10 result = 128 counter = 7 -> 7 < 10 result = 256 counter = 8 -> 8 < 10 result = 512 counter = 9 -> 9 < 10 result = 1024 counter = 10 -> 10 < 10 is not true -> end loop print result -> output: 1024
5th May 2019, 1:05 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
Williams It is just an arrow sign which I use as a pointer/marker. I also use it to represent a chain.
10th May 2019, 9:40 PM
Denise Roßberg
Denise Roßberg - avatar
0
Please what is the meaning of - >
10th May 2019, 9:33 PM
Williams
Williams - avatar