LearnFrenzy
Sign in Take a free test

DOM Manipulation · Exercise 2 of 4

DOM Manipulation Predict the Output

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

Each question shows a small HTML page and a script that runs on it. Choose what the script prints. Every script was run inside its page (jsdom on Node.js, as a browser script), so the answers are the real output.

Question 11 Medium

The page contains this HTML:

<ul id="cart"><li data-sku="PEN-01"><span>Pen</span> <button>Remove</button></li></ul>

What does the following JavaScript code print when it runs on this page?

main.js
const btn = document.querySelector("#cart button");
console.log(btn.closest("li").dataset.sku, btn.closest("#cart").id, btn.closest(".missing"), btn.closest("button") === btn);
A PEN-01 cart undefined false
B PEN-01 cart null true
C undefined cart null true
D PEN-01 null null true
Question page & discussion
Question 12 Hard

The page contains this HTML:

<ul id="a"><li>One</li><li>Two</li></ul><ul id="b"></ul>

What does the following JavaScript code print when it runs on this page?

main.js
const a = document.getElementById("a");
const b = document.getElementById("b");
const first = a.firstElementChild;
b.append(first);
b.append(first);
console.log(a.children.length, b.children.length, b.textContent);
A 1 1 One
B 2 2 OneOne
C 2 1 One
D 1 2 OneOne
Question page & discussion
Question 13 Hard

The page contains this HTML:

<div id="wrap"><p id="p">P</p></div>

What does the following JavaScript code print when it runs on this page?

main.js
const p = document.getElementById("p");
p.insertAdjacentHTML("beforebegin", "<i>1</i>");
p.insertAdjacentHTML("afterbegin", "<i>2</i>");
p.insertAdjacentHTML("beforeend", "<i>3</i>");
p.insertAdjacentHTML("afterend", "<i>4</i>");
console.log(document.getElementById("wrap").innerHTML);
A <i>1</i><p id="p"><i>2</i>P<i>3</i></p><i>4</i>
B <p id="p"><i>1</i><i>2</i>P<i>3</i><i>4</i></p>
C <i>1</i><i>2</i><p id="p">P</p><i>3</i><i>4</i>
D <i>2</i><p id="p"><i>1</i>P<i>4</i></p><i>3</i>
Question page & discussion
Question 14 Medium

The page contains this HTML:

<div id="card" class="card"><h3>Quiz</h3></div>

What does the following JavaScript code print when it runs on this page?

main.js
const card = document.getElementById("card");
const shallow = card.cloneNode();
const deep = card.cloneNode(true);
console.log(`[${shallow.textContent}]`, shallow.className, deep.textContent, deep.isConnected);
A [] card Quiz false
B [Quiz] card Quiz true
C [] undefined Quiz false
D [Quiz] card Quiz false
Question page & discussion
Question 15 Hard

The page contains this HTML:

<div id="box"><button id="go">Go</button></div>

What does the following JavaScript code print when it runs on this page?

main.js
let clicks = 0;
const original = document.getElementById("go");
original.addEventListener("click", () => clicks++);
document.getElementById("box").innerHTML += "<p>Added</p>";
const current = document.getElementById("go");
current.click();
console.log(clicks, current === original);
A 1 true
B 1 false
C 0 true
D 0 false
Question page & discussion
Question 16 Hard

The page contains this HTML:

<a id="link" href="/exam">Start exam</a>

What does the following JavaScript code print when it runs on this page?

main.js
const link = document.getElementById("link");
link.addEventListener("click", (e) => e.preventDefault());
const a = link.dispatchEvent(new Event("click", { cancelable: true }));
const b = link.dispatchEvent(new Event("click"));
console.log(a, b);
A true true
B false false
C true false
D false true
Question page & discussion
Question 17 Hard

The page contains this HTML:

<section id="outer">
  <div id="inner"><button id="btn">Click</button></div>
</section>

What does the following JavaScript code print when it runs on this page?

main.js
const btn = document.getElementById("btn");
document.getElementById("outer").addEventListener("score", (e) => console.log("outer got", e.detail.points));
btn.addEventListener("score", (e) => console.log("button got", e.detail.points));
btn.dispatchEvent(new CustomEvent("score", { detail: { points: 5 } }));
btn.dispatchEvent(new CustomEvent("score", { detail: { points: 10 }, bubbles: true }));
A button got 5 button got 10 outer got 10
B button got 5 outer got 5 button got 10 outer got 10
C button got 5 button got 10
D outer got 10 button got 5 button got 10
Question page & discussion
Question 18 Hard

The page contains this HTML:

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

What does the following JavaScript code print when it runs on this page?

main.js
const menu = document.getElementById("menu");
new MutationObserver((records) => console.log("observer:", records.length, "change(s)"))
  .observe(menu, { childList: true });
setTimeout(() => console.log("timeout"), 0);
menu.append(document.createElement("li"));
menu.append(document.createElement("li"));
console.log("sync done");
A observer: 1 change(s) observer: 1 change(s) sync done timeout
B sync done observer: 2 change(s) timeout
C sync done timeout observer: 2 change(s)
D sync done observer: 1 change(s) timeout
Question page & discussion
Question 19 Medium

The page contains this HTML:

<template id="row"><li class="topic"></li></template><ul id="topics"></ul>

What does the following JavaScript code print when it runs on this page?

main.js
const tpl = document.getElementById("row");
const list = document.getElementById("topics");
for (const name of ["Closures", "Promises", "DOM"]) {
  const li = tpl.content.cloneNode(true).querySelector("li");
  li.textContent = name;
  list.append(li);
}
console.log(list.children.length, tpl.content.children.length, document.querySelectorAll(".topic").length);
console.log(list.lastElementChild.outerHTML);
A 3 0 3 <li class="topic">DOM</li>
B 3 1 3 <li class="topic">DOM</li>
C 3 1 4 <li class="topic">DOM</li>
D 1 1 1 <li class="topic">Closures</li>
Question page & discussion
Question 20 Medium

The page contains this HTML:

<div id="note" style="color: red;">Note</div>

What does the following JavaScript code print when it runs on this page?

main.js
const note = document.getElementById("note");
note.style.fontSize = "18px";
note.style.width = 200;                 // a number, no unit
console.log(note.getAttribute("style"));
console.log(`[${note.style.width}]`, note.style.color);
A color: red; font-size: 18px; width: 200px; [200px] red
B color: red; font-size: 18px; [] red
C font-size: 18px; [] red
D color: red; font-size: 18px; width: 200; [200] red
Question page & discussion
Up next · Exercise 3Find the Error15 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links