0
0
Javascriptprogramming~10 mins

Common array operations in Javascript - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Apush
Bpop
Cunshift
Dshift
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using pop instead of push removes the last element.
Using shift removes the first element instead of adding.
2fill in blank
medium

Complete 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'
Amap
Bfilter
Creduce
DforEach
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.
3fill in blank
hard

Fix 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'
Areduce
Bfilter
Cmap
Dfind
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using filter returns an array, not a single element.
Using map transforms all elements, not find one.
4fill in blank
hard

Fill 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'
Afilter
Bmap
Creduce
Dfind
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using find instead of filter returns only one element.
Using reduce does not return an array.
5fill in blank
hard

Fill 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'
Areduce
Bfruit
Dmap
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using map instead of reduce does not accumulate counts.
Using different keys instead of fruit causes errors.