0
0
Javascriptprogramming~20 mins

While loop in Javascript - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
While Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple while loop
What will be the output of the following JavaScript code?
Javascript
let count = 0;
let result = '';
while (count < 3) {
  result += count + '-';
  count++;
}
console.log(result);
A"0-1-2-3-"
B"0-1-2-"
C"1-2-3-"
D"-0-1-2"
Attempts:
2 left
💡 Hint
Remember the loop runs while count is less than 3, and count starts at 0.
Predict Output
intermediate
2:00remaining
While loop with break statement
What will be printed by this code?
Javascript
let i = 1;
let output = '';
while (i <= 5) {
  if (i === 3) break;
  output += i;
  i++;
}
console.log(output);
A"12345"
B"123"
C""
D"12"
Attempts:
2 left
💡 Hint
The loop stops when i equals 3.
Predict Output
advanced
2:00remaining
While loop with nested condition
What is the final value of variable sum after running this code?
Javascript
let n = 5;
let sum = 0;
while (n > 0) {
  if (n % 2 === 0) {
    sum += n;
  }
  n--;
}
A8
B10
C6
D0
Attempts:
2 left
💡 Hint
Add only even numbers from 5 down to 1.
Predict Output
advanced
2:00remaining
While loop with post-increment in condition
What will this code output?
Javascript
let x = 0;
let output = '';
while (x++ < 3) {
  output += x;
}
console.log(output);
A"123"
B"234"
C"1234"
D"012"
Attempts:
2 left
💡 Hint
Remember x++ returns the value before incrementing.
Predict Output
expert
2:00remaining
While loop with complex condition and variable update
What is the output of this code snippet?
Javascript
let a = 1;
let b = 10;
let result = '';
while (a < b) {
  result += a + ',';
  a *= 2;
  b -= 3;
}
console.log(result);
A"1,2,"
B"1,2,4,8,"
C"1,2,4,"
D"1,2,4,8,16,"
Attempts:
2 left
💡 Hint
Check how a and b change each loop and when the condition fails.