0
0
Javascriptprogramming~10 mins

While loop in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a while loop.

Javascript
let i = 1;
while (i [1] 5) {
  console.log(i);
  i++;
}
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' which stops at 4.
Using 'i > 5' which never runs the loop.
2fill in blank
medium

Complete the code to stop the loop when the variable 'count' reaches 10.

Javascript
let count = 0;
while (count [1] 10) {
  console.log(count);
  count++;
}
Drag options to blanks, or click blank then click option'
A<
B>
C<=
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count <= 10' which runs one extra time.
Using 'count > 10' which never runs the loop.
3fill in blank
hard

Fix the error in the loop condition to avoid an infinite loop.

Javascript
let n = 5;
while (n [1] 0) {
  console.log(n);
  n--;
}
Drag options to blanks, or click blank then click option'
A<=
B>
C>=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'n >= 0' which runs one extra time including 0.
Using 'n < 0' which never runs the loop.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

Javascript
let num = [1];
while (num [2] 10) {
  console.log(num);
  num += 2;
}
Drag options to blanks, or click blank then click option'
A2
B<=
C<
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 which prints 0 as well.
Using '<' which stops before printing 10.
5fill in blank
hard

Fill all three blanks to create a loop that counts down from 9 to 1.

Javascript
let i = [1];
while (i [2] [3]) {
  console.log(i);
  i--;
}
Drag options to blanks, or click blank then click option'
A9
B>
C0
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which would never run the loop.
Using 1 instead of 0 which stops too early.