- 1

Function

The following this code will result in want value? function test(number) { while(number < 5){ number++; } return number; } alert(test(2)); I wondered how can the result of this code return "5" instead of 3 cause the number is 2+1 should be 3

4th Mar 2017, 2:53 AM
Sara
Sara - avatar
1 Odpowiedź
+ 2
It returns 5. In first while loop number is 2 which is less than 5 2++ = 3 -> number Now number is 3, it enters the loop as 3<5 is true 3++ = 4 -> number Now number is 4, it enters the loop again since 4<5 is true 4++ = 5 -> number Now number is 5, it fails the condition in while I.e, 5<5 is false and it comes out of the loop and returns 5. Hope it is clear.
4th Mar 2017, 3:04 AM
Imtiyaz
Imtiyaz - avatar