0
0
Javascriptprogramming~20 mins

Why arrays are needed in Javascript - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use arrays instead of separate variables?

Imagine you want to store the names of 5 friends. Which reason best explains why using an array is better than using 5 separate variables?

AArrays prevent any changes to the stored names once added.
BArrays automatically sort the names alphabetically without extra code.
CArrays use less memory than individual variables for each name.
DArrays let you store many items in one place and easily access them by position.
Attempts:
2 left
πŸ’‘ Hint

Think about how you would find the third friend's name if you had 5 separate variables.

❓ Predict Output
intermediate
2:00remaining
Accessing array elements

What is the output of this JavaScript code?

const fruits = ['apple', 'banana', 'cherry'];
console.log(fruits[1]);
Abanana
Bundefined
Ccherry
Dapple
Attempts:
2 left
πŸ’‘ Hint

Remember, array indexes start at 0.

❓ Predict Output
advanced
2:00remaining
Array length after push

What will be the value of numbers.length after running this code?

const numbers = [1, 2, 3];
numbers.push(4);
numbers.push(5);
console.log(numbers.length);
A5
B4
C3
D6
Attempts:
2 left
πŸ’‘ Hint

Each push adds one item to the array.

🧠 Conceptual
advanced
2:00remaining
Why arrays help with loops

Why are arrays useful when you want to repeat an action for many items?

ABecause arrays store only numbers which are easy to loop over.
BBecause arrays automatically run loops for you without code.
CBecause arrays allow you to use loops to process each item easily by index.
DBecause arrays prevent errors when looping through items.
Attempts:
2 left
πŸ’‘ Hint

Think about how you would print each item in a list of names.

❓ Predict Output
expert
2:00remaining
Array mutation and output

What is the output of this code?

const colors = ['red', 'green', 'blue'];
colors[1] = 'yellow';
console.log(colors.join('-'));
Ared-green-blue
Bred-yellow-blue
Cred,green,blue
Dred,yellow,blue
Attempts:
2 left
πŸ’‘ Hint

Changing an array element updates that position. join('-') connects items with dashes.