0
0
Javascriptprogramming~10 mins

Nested loops 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 2 using a loop.

Javascript
for (let i = 1; i [1] 3; 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 '>=' causes the loop not to run.
Using '>' makes the loop never start.
2fill in blank
medium

Complete the inner loop to print pairs of i and j from 1 to 2.

Javascript
for (let i = 1; i <= 2; i++) {
  for (let j = 1; j [1] 3; j++) {
    console.log(i, j);
  }
}
Drag options to blanks, or click blank then click option'
A<
B<=
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' prints j = 1, 2, and 3, which is more than needed.
Using '>=' or '>' causes the loop not to run.
3fill in blank
hard

Fix the error in the nested loops to print all pairs from 1 to 3.

Javascript
for (let x = 1; x <= 3; x++) {
  for (let y = 1; y [1] 3; y++) {
    console.log(x, y);
  }
}
Drag options to blanks, or click blank then click option'
A<
B>=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes the last number 3.
Using '>' or '>=' causes the loop not to run.
4fill in blank
hard

Fill both blanks to create a nested loop that prints a 3x3 grid of stars.

Javascript
for (let row = 1; row [1] 3; row++) {
  let line = '';
  for (let col = 1; col [2] 3; col++) {
    line += '* ';
  }
  console.log(line);
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes only 2 rows or columns to print.
Using '>' or '>=' causes loops not to run.
5fill in blank
hard

Fill all three blanks to create a nested loop that prints the multiplication table from 1 to 3.

Javascript
for (let i = 1; i [1] 3; i++) {
  for (let j = 1; j [2] 3; j++) {
    console.log(`$[3] * ${j} = ${i * j}`);
  }
}
Drag options to blanks, or click blank then click option'
A<
B<=
Ci
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes the last number 3.
Using 'j' instead of 'i' in the output causes wrong multiplication.