'How does this while block work? [closed]
var result = 1
var counter = 0
while (counter < 10) {
result = result * 2
counter += 1
};
console.log(result);
I am confused how does counter update result here? We are increasing counter by 1 but how does that affect the result?
Can someone please dumb it down to me? I am new to programming.
Edit : I know this question has been asked many times. I searched through many answers but didn't get the info I required. I have a very specific doubt and wanted to clarify it so please go easy on that down button. :)
[Solved]
Same code with for loop.
var result = 1
for (counter = 0; counter < 10; counter++) {
result *= 2;
};
console.log(result);
Solution 1:[1]
You mean this:
loop | counter | result | counter < 10
1 1 2 yes
2 2 4 yes
3 3 8 yes
4 4 16 yes
5 5 32 yes
6 6 64 yes
7 7 128 yes
8 8 256 yes
9 9 512 yes
10 10 1024 no end of loop
console.log(result); -> 1024
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bugs2919 |
