Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i < 5' which stops at 4.
Using 'i > 5' which never runs the loop.
✗ Incorrect
The condition i <= 5 ensures the loop runs while i is 1 through 5.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count <= 10' which runs one extra time.
Using 'count > 10' which never runs the loop.
✗ Incorrect
Using 'count < 10' stops the loop before count reaches 10, so it prints 0 to 9.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'n >= 0' which runs one extra time including 0.
Using 'n < 0' which never runs the loop.
✗ Incorrect
Using 'n > 0' ensures the loop stops when n reaches 0, preventing an infinite loop.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 which prints 0 as well.
Using '<' which stops before printing 10.
✗ Incorrect
Start at 2 and loop while num is less than or equal to 10 to print even numbers up to 10.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which would never run the loop.
Using 1 instead of 0 which stops too early.
✗ Incorrect
Start at 9 and loop while i is greater than 0 to count down to 1.