LearnFrenzy
Sign in Take a free test

DOM Manipulation · Exercise 3 of 4

DOM Manipulation Find the Error

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

Debug fifteen DOM scripts, each shown with its page: SyntaxErrors from optional-chaining assignment, dashed dataset names and unbalanced listeners; runtime errors from null selections, wrong getElementById arguments, collection methods, non-Node arguments and string data values; and silent bugs from innerHTML XSS, lost listeners, e.target in delegation, live collections, className checks and missing units.

Question 1 Medium

The page contains this HTML:

<h1 id="title">Old</h1>

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
document.querySelector("#title")?.textContent = "Welcome back";
A SyntaxError: an optional chain cannot be the target of an assignment – check for null first (const el = …; if (el) el.textContent = …)
B It sets the title to Welcome back
C It does nothing when the element is missing
D TypeError at run time
Question page & discussion
Question 2 Medium

The page contains this HTML:

<div id="profile"></div>

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const profile = document.getElementById("profile");
profile.dataset.user-id = "42";
A It sets data-user-id="42"
B It sets data-user="42"
C ReferenceError: id is not defined
D SyntaxError: it parses as profile.dataset.user - id = "42", an invalid assignment target – use dataset.userId
Question page & discussion
Question 3 Easy

The page contains this HTML:

<button id="save">Save</button>

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
document.getElementById("save").addEventListener("click", () => {
  console.log("saving…");
};
A It logs saving… on every click
B It logs saving… once at load
C TypeError: addEventListener is not a function
D SyntaxError: the addEventListener call is never closed – the last line must be });
Question page & discussion
Question 4 Easy

The page contains this HTML:

<button id="submitBtn">Submit</button>

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const btn = document.querySelector("#submit-btn");
btn.addEventListener("click", () => console.log("submitted"));
A It logs submitted
B Nothing happens, silently
C SyntaxError: invalid selector
D TypeError: querySelector returns null (the id is submitBtn, not submit-btn), and null has no addEventListener
Question page & discussion
Question 5 Easy

The page contains this HTML:

<h2 id="score">0</h2>

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const score = document.getElementById("#score");
score.textContent = "95";
A It sets the score to 95
B TypeError: getElementById takes the bare id ("score"); with "#score" it finds nothing and returns null
C SyntaxError: # is not allowed in strings
D It creates a new element with id #score
Question page & discussion
Question 6 Medium

The page contains this HTML:

<ul id="tasks">
  <li class="task done">Read</li>
  <li class="task">Code</li>
  <li class="task done">Test</li>
</ul>

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const tasks = document.getElementsByClassName("task");
tasks.forEach((t) => t.classList.add("seen"));
A It adds the class to every task
B It adds the class to the first task only
C SyntaxError
D TypeError: tasks.forEach is not a function – an HTMLCollection has no forEach; use [...tasks].forEach or querySelectorAll
Question page & discussion
Question 7 Medium

The page contains this HTML:

<ul id="tasks">
  <li class="task done">Read</li>
  <li class="task">Code</li>
  <li class="task done">Test</li>
</ul>

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const names = document.querySelectorAll(".task").map((li) => li.textContent);
console.log(names.join(", "));
A It prints Read, Code, Test
B TypeError: a NodeList has forEach but not map – write [...document.querySelectorAll(".task")].map(…) or Array.from(list, fn)
C It prints an empty line
D It prints [object NodeList]
Question page & discussion
Question 8 Medium

The page contains this HTML:

<ul id="menu">
  <li>Home</li>
  <li>Courses</li>
</ul>

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const menu = document.getElementById("menu");
menu.appendChild("<li>Blog</li>");
A TypeError: appendChild needs a Node, not a string – create the element (or use insertAdjacentHTML / append with text)
B It adds a Blog item
C It adds the text "<li>Blog</li>" as plain text
D SyntaxError
Question page & discussion
Question 9 Medium

The page contains this HTML:

<button id="buy" data-price="499.5">Buy</button>

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const buy = document.getElementById("buy");
buy.addEventListener("click", (e) => {
  console.log("Pay ₹" + e.currentTarget.dataset.price.toFixed(2));
});
console.log("clicking…");
buy.click();
A It prints Pay ₹499.50
B TypeError inside the click listener: dataset values are strings, and strings have no toFixed – use Number(…).toFixed(2)
C It prints Pay ₹499.5
D SyntaxError
Question page & discussion
Question 10 Medium

The page contains this HTML:

<div id="comments"></div>

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

main.js
function addComment(author, text) {
  const div = document.createElement("div");
  div.innerHTML = `<b>${author}</b>: ${text}`;
  document.getElementById("comments").append(div);
}
addComment("Ravi", 'Great! <img src="x" onerror="alert(document.cookie)">');
console.log("images in comments:", document.querySelectorAll("#comments img").length);
A The comment should use single quotes
B append should be appendChild
C User text is inserted with innerHTML, so its markup becomes a real <img> with an onerror script (XSS) – set text with textContent
D createElement should be createTextNode for the div
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links