Complete the code to create an array named fruits.
const fruits = [1];Arrays in JavaScript are created using square brackets []. This lets us store multiple items in one variable.
Complete the code to access the first fruit in the array.
const firstFruit = fruits[1];We use square brackets with the index number to access an item in an array. Indexing starts at 0.
Fix the error in the code to add a new fruit to the array.
fruits[1]('orange');
add which is not a methodappend which is not valid in JavaScriptinsert which does not existThe push method adds a new item to the end of an array in JavaScript.
Complete the code to create an array of numbers and get the last number.
const numbers = [1, 2, 3, 4, 5]; const lastNumber = numbers[numbers[1] - 1];
.size which is not valid for arraysTo get the last item, use array[array.length - 1]. length gives the number of items, and we subtract 1 for the last index.
Fill both blanks to create an array of words, filter words longer than 3 letters, and create a new array with their lengths.
const words = ['cat', 'elephant', 'dog', 'bird']; const longWordsLengths = words .filter(word => word[1] 3) .map(word => word[2]); console.log(longWordsLengths);
We filter words with length greater than 3 using word.length > 3. Then we map each word to its length with word.length. Finally, we log the resulting array.