The length property returns the number of elements in the array.
Step 2: Count elements in the array
The array has 3 elements: 10, 20, and 30, so arr.length is 3.
Final Answer:
3 -> Option D
Quick Check:
Array length = 3 [OK]
Hint: Array length counts items, not values [OK]
Common Mistakes:
Confusing length with last element value
Expecting length to be undefined
Thinking length causes an error
4. Find the error in this array creation code:
const fruits = ["apple", "banana", "orange";
medium
A. Missing comma between elements
B. Strings should use single quotes instead of double quotes
C. Missing closing square bracket ]
D. Array elements must be numbers only
Solution
Step 1: Check array syntax
Arrays must start with [ and end with ]. The code misses the closing bracket.
Step 2: Verify other parts
Double quotes are allowed for strings, commas are present between elements.
Final Answer:
Missing closing square bracket ] -> Option C
Quick Check:
Array syntax requires matching brackets [OK]
Hint: Count opening and closing brackets to spot errors [OK]
Common Mistakes:
Forgetting to close the array with ]
Thinking quotes style causes error
Assuming arrays only hold numbers
5. You want to create an array that contains the numbers from 1 to 5 using a loop. Which code correctly creates this array?
hard
A. const arr = [];
for(let i = 0; i < 5; i++) {
arr[i] = i + 1;
}
B. const arr = [];
for(let i = 1; i <= 5; i++) {
arr[i] = i;
}
C. const arr = new Array(5);
for(let i = 1; i <= 5; i++) {
arr[i] = i;
}
D. const arr = [];
for(let i = 1; i < 5; i++) {
arr.push(i);
}
Solution
Step 1: Understand array creation with loop
We want numbers 1 to 5, so loop must run 5 times and add values 1 through 5.
Step 2: Analyze each option
const arr = [];
for(let i = 1; i <= 5; i++) {
arr[i] = i;
} assigns values starting at index 1, leaving index 0 empty. const arr = new Array(5);
for(let i = 1; i <= 5; i++) {
arr[i] = i;
} creates array of length 5 but assigns starting at index 1, leaving index 0 empty. const arr = [];
for(let i = 0; i < 5; i++) {
arr[i] = i + 1;
} assigns values 1 to 5 at indexes 0 to 4 correctly. const arr = [];
for(let i = 1; i < 5; i++) {
arr.push(i);
} loops only to 4, missing 5.
Step 3: Choose best correct code
const arr = [];
for(let i = 0; i < 5; i++) {
arr[i] = i + 1;
} correctly fills array indexes 0 to 4 with values 1 to 5.
Final Answer:
const arr = [];
for(let i = 0; i < 5; i++) {
arr[i] = i + 1;
} -> Option A
Quick Check:
Loop 0-4, assign i+1 = 1-5 [OK]
Hint: Loop from 0 to length-1, assign i+1 for 1 to 5 [OK]
Common Mistakes:
Starting loop at 1 but assigning to arr[i] leaves arr[0] empty
Loop ending before 5
Using new Array(5) without filling all indexes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.