0
0
Javascriptprogramming~10 mins

Iterating over arrays 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 loop through the array and print each number.

Javascript
const numbers = [1, 2, 3, 4];
for (let [1] = 0; [1] < numbers.length; [1]++) {
  console.log(numbers[[1]]);
}
Drag options to blanks, or click blank then click option'
Ai
Bnum
Cindex
Ditem
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a variable name that is not consistent throughout the loop.
Starting the loop counter at 1 instead of 0.
2fill in blank
medium

Complete the code to use the forEach method to print each fruit.

Javascript
const fruits = ['apple', 'banana', 'cherry'];
fruits.forEach([1] => {
  console.log([1]);
});
Drag options to blanks, or click blank then click option'
Aitem
Bfruit
Cfruits
Dindex
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the plural array name as the parameter.
Using index when the element itself is needed.
3fill in blank
hard

Fix the error in the code to correctly print each element doubled.

Javascript
const nums = [1, 2, 3];
for (const [1] of nums) {
  console.log([1] * 2);
}
Drag options to blanks, or click blank then click option'
Anum
Bnums
Cindex
Di
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the array name instead of the element variable.
Using an index variable in a for...of loop.
4fill in blank
hard

Fill both blanks to create an object mapping each word to its length for words longer than 3 letters.

Javascript
const words = ['cat', 'elephant', 'dog', 'horse'];
const lengths = {};
for (const [1] of words) {
  if ([2].length > 3) {
    lengths[[2]] = [2].length;
  }
};
Drag options to blanks, or click blank then click option'
Aword
Bwords
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using the array name instead of the element variable.
Using different variable names in the two blanks.
5fill in blank
hard

Fill all three blanks to create a new array with squares of even numbers only.

Javascript
const numbers = [1, 2, 3, 4, 5];
const squares = numbers
  .filter([1] => [1] % 2 === 0)
  .map([2] => [2][3]2);
Drag options to blanks, or click blank then click option'
An
C**
D*
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using different variable names in filter and map.
Using * instead of ** for exponentiation.