Complete the code to loop through the array and print each number.
const numbers = [1, 2, 3, 4]; for (let [1] = 0; [1] < numbers.length; [1]++) { console.log(numbers[[1]]); }
We use i as the loop counter variable to access each index of the array.
Complete the code to use the forEach method to print each fruit.
const fruits = ['apple', 'banana', 'cherry']; fruits.forEach([1] => { console.log([1]); });
index when the element itself is needed.The forEach method takes a function with one parameter representing each element. Here, fruit is that parameter.
Fix the error in the code to correctly print each element doubled.
const nums = [1, 2, 3]; for (const [1] of nums) { console.log([1] * 2); }
Using num as the variable name for each element in the array is correct. nums is the array itself.
Fill both blanks to create an object mapping each word to its length for words longer than 3 letters.
const words = ['cat', 'elephant', 'dog', 'horse']; const lengths = {}; for (const [1] of words) { if ([2].length > 3) { lengths[[2]] = [2].length; } };
We use word as the variable for each element. The object maps each word to its length.
Fill all three blanks to create a new array with squares of even numbers only.
const numbers = [1, 2, 3, 4, 5]; const squares = numbers .filter([1] => [1] % 2 === 0) .map([2] => [2][3]2);
filter and map.* instead of ** for exponentiation.We use n as the variable for each number. The filter keeps even numbers, and map squares them using the exponent operator **.