Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add an element to the end of the array.
Javascript
const fruits = ['apple', 'banana']; fruits.[1]('orange'); console.log(fruits);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using pop instead of push removes the last item.
Using shift or unshift changes the start of the array.
β Incorrect
The push method adds an element to the end of an array.
2fill in blank
mediumComplete the code to remove the first element from the array.
Javascript
const numbers = [10, 20, 30]; numbers.[1](); console.log(numbers);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using pop removes the last element, not the first.
Using push or unshift adds elements instead of removing.
β Incorrect
The shift method removes the first element from an array.
3fill in blank
hardFix the error in the code to replace the second element with 'grape'.
Javascript
const fruits = ['apple', 'banana', 'cherry']; fruits[[1]] = 'grape'; console.log(fruits);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using index 2 changes the third element, not the second.
Using string indexes causes errors.
β Incorrect
Array indexes start at 0, so the second element is at index 1.
4fill in blank
hardFill both blanks to create a new array with squares of even numbers only.
Javascript
const numbers = [1, 2, 3, 4, 5]; const squares = numbers.filter(n => n [1] 2 === 0).map(n => n [2] 2); console.log(squares);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using '+' instead of '**' adds numbers instead of squaring.
Using '===' in filter instead of '%' causes wrong filtering.
β Incorrect
The filter uses % 2 === 0 to find even numbers. The map uses ** 2 to square them.
5fill in blank
hardFill all three blanks to create an object mapping words to their lengths, only for words longer than 3 letters.
Javascript
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);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using 'for' instead of 'in' in the loop causes syntax errors.
Using 'for' as a key or value is incorrect.
β Incorrect
This uses a for...in loop to iterate over array indexes and map words to their lengths if longer than 3 letters.