Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' causes the loop not to run.
Using '>' makes the loop never start.
✗ Incorrect
The loop should run while i is less than 3 to print 1 and 2. Using '<' is correct.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The inner loop should run while j is less than 3 to print pairs with j = 1 and 2.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes the last number 3.
Using '>' or '>=' causes the loop not to run.
✗ Incorrect
Using '<=' ensures y goes from 1 to 3 inclusive, printing all pairs.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes only 2 rows or columns to print.
Using '>' or '>=' causes loops not to run.
✗ Incorrect
Both loops should run from 1 to 3 inclusive to print a 3x3 grid.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' excludes the last number 3.
Using 'j' instead of 'i' in the output causes wrong multiplication.
✗ Incorrect
Both loops run from 1 to 3 inclusive, and the outer loop variable i is used in the output.