LearnFrenzy
Sign in Take a free test

Arrays · Exercise 3 of 4

Arrays Find the Error

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen JavaScript array programs: SyntaxErrors in literals and destructuring, RangeErrors and TypeErrors from invalid lengths, missing arrays, frozen arrays and non-iterables, and silent bugs from default sort, the fill trap, shared references, === comparisons and indexOf checks.

Question 11 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const scores = [9, 85, 100, 42];
scores.sort();
console.log("Highest:", scores[scores.length - 1]);
A scores[scores.length - 1] is out of range
B sort() compares numbers as strings, so "9" ends up last – use scores.sort((a, b) => a - b)
C sort() sorts in descending order
D Arrays of numbers cannot be sorted
Question page & discussion
Question 12 Hard

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const seats = new Array(3).fill(new Array(3).fill("."));
seats[0][1] = "X";                  // book row 0, seat 1
console.log(seats.map((row) => row.join("")).join("\n"));
A join("") changes the seats
B Rows must be strings
C fill puts the same inner array in every row, so changing one row changes all – build rows with Array.from({ length: 3 }, () => new Array(3).fill("."))
D Indexes start at 1
Question page & discussion
Question 13 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const todo = ["email", "report"];
const backup = todo;
todo.push("meeting");
console.log("Backup:", backup);
A push adds to every array
B const arrays are shared automatically
C backup = todo copies the reference, so both names show the same array – copy with [...todo]
D console.log prints the newest array
Question page & discussion
Question 14 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const correct = ["B", "D", "A"];
const answered = ["B", "D", "A"];
console.log(answered === correct ? "All correct" : "Some answers wrong");
A Strings in arrays cannot be compared
B The arrays must be sorted first
C === compares references, not contents – compare lengths and each element: a.every((x, i) => x === b[i])
D The ternary needs parentheses
Question page & discussion
Question 15 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const roles = ["admin", "editor", "viewer"];
function hasRole(role) {
  return roles.indexOf(role) ? "yes" : "no";
}
console.log(hasRole("admin"), hasRole("editor"), hasRole("guest"));
A indexOf is case-sensitive
B indexOf returns 0 for "admin" (falsy) and -1 for missing roles (truthy) – use roles.includes(role)
C The roles array must be sorted
D The function should use ===
Question page & discussion
Up next · Exercise 4Arrays Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links