0
why would this JavaScript code block return 5?
function test(number) { while(number < 5) { number++; } return number; } alert(test(2)); // why does the answer return to 5? i thought while 2 < 5 then we will get 3+1 = 4 and our number will be 4.. i dont get it
3 ответов
+ 2
4 is smaller than 5 so it will increment to 5 then the condition is false therefore returns 5
+ 4
n = 2 < 5 => true => now n = n + 1 = 3
n = 3 < 5 => true => now n = n + 1 = 4
n = 4 < 5 => true => now n = n + 1 = 5
n = 5 < 5 => false
And now function will return number = 5.
Hope this helps ☺️☺️.
0
all great answers thanks guys