0

what us the output of the code below plz explain it briefly I don't understand how the output is 5

function test(x){ while(x<5){ x++; } return x; } alert(test(2))

23rd Jul 2020, 3:42 PM
Beamlak Amare
Beamlak Amare - avatar
5 Answers
+ 1
The function keeps up adding one to the number until it reaches five and thus it returns five as the output.
23rd Jul 2020, 3:48 PM
Saphal Poudyal
Saphal Poudyal - avatar
+ 2
Beamlak Amare , the while loop runs until false condition is met. When the value becomes 5 the loop stops and the function returns the value which is shown in the alert box.
23rd Jul 2020, 3:48 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Beamlak Amare , the last step is when x is 4 => 4 < 5 is true, x is increased and becomes 5. But 5 < 5 is false => the loop stops.
23rd Jul 2020, 4:09 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
but why does it output 5 because x<5
23rd Jul 2020, 3:53 PM
Beamlak Amare
Beamlak Amare - avatar
0
so it outputs 5 because it is where it stops?
24th Jul 2020, 5:37 AM
Beamlak Amare
Beamlak Amare - avatar