0
0
Javascriptprogramming~10 mins

Accessing array elements 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 access the first element of the array.

Javascript
const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[[1]]);
Drag options to blanks, or click blank then click option'
A0
B1
C2
D3
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 1 instead of 0 to access the first element.
Using an index that is out of range.
2fill in blank
medium

Complete the code to access the last element of the array.

Javascript
const colors = ['red', 'green', 'blue', 'yellow'];
console.log(colors[colors.length [1] 1]);
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Adding 1 instead of subtracting 1 from length.
Using length directly as index.
3fill in blank
hard

Fix the error in the code to correctly access the second element.

Javascript
const numbers = [10, 20, 30, 40];
console.log(numbers[[1]]);
Drag options to blanks, or click blank then click option'
A1
B2
C0
D3
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using index 2 to access the second element.
Using index 0 which is the first element.
4fill in blank
hard

Fill both blanks to create an array of squares for numbers greater than 2.

Javascript
const numbers = [1, 2, 3, 4, 5];
const squares = numbers.filter(n => n [1] 2).map(n => n [2] n);
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 '>' in filter.
Using '**' which is exponentiation but less common in JS for this task.
5fill in blank
hard

Fill all three blanks to create an object mapping each fruit to its length if length is greater than 5.

Javascript
const fruits = ['apple', 'banana', 'cherry', 'date'];
const result = {};
for (let [1] of fruits) {
  if ([1].length > 5) {
    result[[1]] = [2];
  }
}
console.log(result);
Drag options to blanks, or click blank then click option'
Afruit
Bfruit.length
Cfor
Dof
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'for ... in' instead of 'for ... of'.
Using fruit instead of fruit.length for the value.