0
0
Javascriptprogramming~10 mins

Loop execution flow 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 <= 5 will print 0 to 5, which is one extra number.
Using > or >= will not run the loop as expected.
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 < 5 will exclude 5 from the sum.
Using > or >= will not run the loop correctly.
3fill in blank
hard

Fix the error in the loop condition to print even numbers from 2 to 10.

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

Fill both blanks to create an object with numbers as keys and their squares as values for numbers 1 to 4.

Javascript
const squares = {};
for (let n = 1; n <= 4; n++) {
  squares[[1]] = [2];
}
Drag options to blanks, or click blank then click option'
An
Bn * n
Cn ** 2
Dn + n
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + n gives double the number, not square.
Using n * n is correct but not the expected answer here.
5fill in blank
hard

Fill all three blanks to create an object with uppercase keys and values greater than 2 from the data object.

Javascript
const filtered = {};
for (const [key, value] of Object.entries(data)) {
  if (value [3] 2) {
    filtered[[1]] = [2];
  }
}
Drag options to blanks, or click blank then click option'
Akey.toUpperCase()
Bvalue
C>
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using <= includes values less than or equal to 2, which is wrong.
Not converting keys to uppercase.