0
0
Javascriptprogramming~10 mins

Why loops are needed in Javascript - Test Your Understanding

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 3 using a loop.

Javascript
for(let i = 1; i [1] 4; 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 never run the loop.
Using '==' will only run when i equals 4.
2fill in blank
medium

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

Javascript
let sum = 0;
for(let num = 1; num [1] 6; 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 '<' will exclude 5, which is needed.
Using '>' will not run the loop.
3fill in blank
hard

Fix the error in the loop condition to print 'Hello' 3 times.

Javascript
for(let count = 0; count [1] 3; count++) {
  console.log('Hello');
}
Drag options to blanks, or click blank then click option'
A<
B>=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' will cause the loop to never run.
Using '==' will only run once.
4fill in blank
hard

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

Javascript
for(let i = [1]; i [2] 11; i += 2) {
  console.log(i);
}
Drag options to blanks, or click blank then click option'
A2
B<
C<=
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 1 will print odd numbers.
Using '<=' will include 12 which is not needed.
5fill in blank
hard

Fill all three blanks to create a loop that prints numbers from 5 down to 1.

Javascript
for(let num = [1]; num [2] 0; num [3]) {
  console.log(num);
}
Drag options to blanks, or click blank then click option'
A5
B>
C--
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will cause the loop to never run.
Using '++' will increase instead of decrease.