Complete the code to add an element to the end of the array.
const fruits = ['apple', 'banana']; fruits.[1]('orange'); console.log(fruits);
The push method adds an element to the end of an array.
Complete the code to remove the first element from the array.
const numbers = [10, 20, 30]; numbers.[1](); console.log(numbers);
The shift method removes the first element from an array.
Fix the error in the code to replace the second element with 'grape'.
const fruits = ['apple', 'banana', 'cherry']; fruits[[1]] = 'grape'; console.log(fruits);
Array indexes start at 0, so the second element is at index 1.
Fill both blanks to create a new array with squares of even numbers only.
const numbers = [1, 2, 3, 4, 5]; const squares = numbers.filter(n => n [1] 2 === 0).map(n => n [2] 2); console.log(squares);
The filter uses % 2 === 0 to find even numbers. The map uses ** 2 to square them.
Fill all three blanks to create an object mapping words to their lengths, only for words longer than 3 letters.
const words = ['cat', 'elephant', 'dog', 'lion']; const lengths = {}; for (let index [3] words) { const word = words[index]; if (word.length > 3) { lengths[[1]] = [2]; } } console.log(lengths);
This uses a for...in loop to iterate over array indexes and map words to their lengths if longer than 3 letters.
