0
0
Javascriptprogramming~10 mins

For 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 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'
A==
B<=
C>
D<
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.
2fill in blank
medium

Complete 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'
A<
B>
C<=
D==
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.
3fill in blank
hard

Fix 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'
A<=
B<
C>
D==
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.
4fill in blank
hard

Fill 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'
A5
B>
C>=
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at 0 will not count down from 5.
Using '>=' will print 0 as well.
5fill in blank
hard

Fill 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'
A2
B<=
C+= 2
D++
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.