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 element.
Using shift removes the first element instead of adding.
β Incorrect
The push method adds an element to the end of an array.
2fill in blank
mediumComplete the code to create a new array with each number doubled.
Javascript
const numbers = [1, 2, 3]; const doubled = numbers.[1](n => n * 2); console.log(doubled);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using filter returns only elements that pass a test, not transformed elements.
Using forEach does not create a new array.
β Incorrect
The map method creates a new array by applying a function to each element.
3fill in blank
hardFix the error in the code to find the first number greater than 10.
Javascript
const nums = [4, 9, 15, 7]; const first = nums.[1](n => n > 10); console.log(first);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using filter returns an array, not a single element.
Using map transforms all elements, not find one.
β Incorrect
The find method returns the first element that matches the condition.
4fill in blank
hardFill both blanks to create an array of squares for numbers greater than 3.
Javascript
const nums = [1, 2, 3, 4, 5]; const squares = nums.[1](n => n > 3).[2](n => n * n); console.log(squares);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using find instead of filter returns only one element.
Using reduce does not return an array.
β Incorrect
First, filter selects numbers greater than 3, then map squares them.
5fill in blank
hardFill all three blanks to create an object counting occurrences of each fruit.
Javascript
const fruits = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']; const count = fruits.[1]((acc, fruit) => { acc[[2]] = (acc[[3]] || 0) + 1; return acc; }, {}); console.log(count);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
π‘ Hint
Common Mistakes
Using map instead of reduce does not accumulate counts.
Using different keys instead of fruit causes errors.
β Incorrect
reduce builds an object counting each fruit. The key is the fruit name used twice.