Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print numbers from 0 to 4 using a for loop.
Javascript
for (let i = 0; i [1] 5; i++) { console.log(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' will print 0 to 5, which is one extra number.
Using '==' or '>' will not create a proper loop condition.
✗ Incorrect
The loop should run while i is less than 5 to print numbers 0 to 4.
2fill in blank
mediumComplete the code to sum numbers from 1 to 5 using a for loop.
Javascript
let sum = 0; for (let num = 1; num [1] 5; num++) { sum += num; } console.log(sum);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes 5, so sum will be less than expected.
Using '==' will only run the loop once when num equals 5.
✗ Incorrect
Using '<=' includes 5 in the sum, so numbers 1 through 5 are added.
3fill in blank
hardFix the error in the for loop to print each fruit in the array.
Javascript
const fruits = ['apple', 'banana', 'cherry']; for (let i = 0; i [1] fruits.length; i++) { console.log(fruits[i]); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes an error because fruits[fruits.length] is undefined.
Using '>' or '==' will not loop through the array correctly.
✗ Incorrect
The loop should run while i is less than fruits.length to avoid going out of bounds.
4fill in blank
hardFill both blanks to create a for loop that counts down from 5 to 1.
Javascript
for (let i = [1]; i [2] 0; i--) { console.log(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 will not count down from 5.
Using '>=' will print 0 as well.
✗ Incorrect
Start at 5 and continue while i is greater than 0 (i > 0).
5fill in blank
hardFill all three blanks to create a for loop that prints even numbers from 2 to 10.
Javascript
for (let i = [1]; i [2] 10; i [3]) { console.log(i); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '++' increases by 1, so odd numbers will be printed.
Starting at 0 or 1 will not print the correct even numbers.
✗ Incorrect
Start at 2, loop while i is less than or equal to 10, and increase i by 2 each time.