+ 6

I don't understand why the output is 5, Here please explain thanks

The following code will result in what value? function test(number) { while(number < 5) { number++; } return number; } alert(test(2));

20th Dec 2016, 3:20 PM
Making A Change
Making A Change - avatar
18 ответов
+ 15
In your example , you enter while 2<5 --> True, 2++---> number is 3 , 3<5 ---> True , 3++ --> number is 4, 4<5 --> True , 4++ ---> number is 5. 5<5 ---> FALSE , exit WHILE. So number is 5
20th Dec 2016, 3:34 PM
Majstor_Gile
Majstor_Gile - avatar
+ 13
Because while number is less then 5 variable will increment. It will reach 5 and stop.
20th Dec 2016, 3:22 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
First you will call alert(var1) that calls your function test(var2) and sends var2 ( in your example 2) to this function as argument. In function test you have while loop which you can look in 3 steps. STEP 1: Ask condition (number < 5) and if FALSE exit while, and if TRUE then STEP 2: execute statements ( you have only number++) and STEP 3: Loop again to STEP 1.
20th Dec 2016, 3:30 PM
Majstor_Gile
Majstor_Gile - avatar
+ 3
A box will pop-up containing the return value of number (witch is based on the arguments you pass (here you pass 2))
20th Dec 2016, 3:29 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 3
the "alert(test(2))" means that you want to create an alert window that will show the the result (the returned number) when the test function is run with a 2 passed in as the parameter.
20th Dec 2016, 4:28 PM
Dustin
Dustin - avatar
+ 2
ok thanks what does the alert(test(2)); mean? its mean that your function start from 2 function test(number) = function test(2)
9th Jul 2018, 10:31 AM
Ivan Bilous
+ 1
In your example , you enter while 2<5 --> True, 2++---> number is 3 , 3<5 ---> True , 3++ --> number is 4, 4<5 --> True , 4++ ---> number is 5. 5<5 ---> FALSE , exit WHILE. So number is 5
9th Apr 2020, 12:15 PM
Druide LEKOGHO
Druide LEKOGHO - avatar
+ 1
The following code will result in what value? function test(number) { while(number < 5) { number++; } return number; } alert(test(2)); Answare == 5
23rd Jul 2020, 4:45 AM
Farhad Rahmani
Farhad Rahmani - avatar
0
ok thanks what does the alert(test(2)); mean?
20th Dec 2016, 3:26 PM
Making A Change
Making A Change - avatar
0
Thank You Druide for the explanation
9th Apr 2020, 1:11 PM
Making A Change
Making A Change - avatar
0
thanks for the explination
29th Sep 2020, 2:45 PM
Mehdi idrissi
Mehdi idrissi - avatar
0
yeah the output is five
8th Oct 2020, 12:01 PM
Davi Fletcher
Davi Fletcher - avatar
0
thanks
16th Jul 2021, 6:30 AM
Woraya Huadphithak
Woraya Huadphithak - avatar
0
5
25th Mar 2022, 1:30 PM
Roselle Mae Santos
0
5
11th Nov 2022, 3:12 PM
Daniela Bulimar
Daniela Bulimar - avatar
0
The answer will be 5
1st Apr 2023, 10:10 AM
Arpita Karmakar
Arpita Karmakar - avatar
- 1
5
16th Sep 2019, 7:16 AM
Jean de Dieu RUMANZI
Jean de Dieu RUMANZI - avatar
- 1
5
31st Oct 2022, 3:37 AM
tristan asfar
tristan asfar - avatar