0
0
Javascriptprogramming~10 mins

Modifying arrays 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'
Aunshift
Bpop
Cpush
Dshift
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.
2fill in blank
medium

Complete 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'
Ashift
Bpush
Cpop
Dunshift
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using pop removes the last element, not the first.
Using push or unshift adds elements instead of removing.
3fill in blank
hard

Fix 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'
A'2'
B2
C'1'
D1
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using index 2 changes the third element, not the second.
Using string indexes causes errors.
4fill in blank
hard

Fill 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'
A%
B**
C===
D+
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using '+' instead of '**' adds numbers instead of squaring.
Using '===' in filter instead of '%' causes wrong filtering.
5fill in blank
hard

Fill 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'
Aword
Bword.length
Cfor
Din
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.