Codiqo

Learn JavaScript · lesson 46 of 60 · about 10 minutes

Capstone: Core Logic and the DOM

Write the functions that change Sam's deck, and render the deck on the page.

What you'll learn

  • Write small state-changing functions
  • Render state to the page
  • Understand how the DOM displays it

Flip, mark and move on

The flashcards need three actions: flip(d) turns the card over, mark(d, knewIt) adds the card's id to known (once) or takes it out again, and next(d) moves to the next card, back to the first after the last, face down. Each one only changes the deck. A fourth function, render(d), reads the deck and draws it in two parts of the page: Card 1 of 4: Strict equality operator? (or Card 1 of 4, answer: === when flipped) and Known: 1 of 4, with . All done! once every card is known.

Actions change state

Each user action is a small function: flip(state), mark(state, knewIt) and next(state). They only change the state. They don't touch the screen.

Rendering to the DOM

In a browser, the page is a tree of elements called the DOM. JavaScript finds an element and replaces what's inside it:

  • const app = document.querySelector("#app") finds the element
  • app.innerHTML = render(state) draws the state

Our render function just returns an HTML string, so we can test it here without a browser.

Actions and render

const state = {
  cards: [
    { id: 1, front: "Keyword for a constant?", back: "const" },
    { id: 2, front: "Strict equality?", back: "===" },
  ],
  index: 0,
  flipped: false,
  known: [],
};

const flip = (s) => { s.flipped = !s.flipped; };
const mark = (s, knewIt) => {
  if (knewIt) s.known.push(s.cards[s.index].id);
};
const next = (s) => {
  s.index = (s.index + 1) % s.cards.length;
  s.flipped = false;
};

function render(s) {
  const card = s.cards[s.index];
  const text = s.flipped ? card.back : card.front;
  return `<div class="card">${text}</div><p>Known: ${s.known.length}</p>`;
}

console.log(render(state));
flip(state);
console.log(render(state));
mark(state, true);
next(state);
console.log(render(state));

Output

<div class="card">Keyword for a constant?</div><p>Known: 0</p>
<div class="card">const</div><p>Known: 0</p>
<div class="card">Strict equality?</div><p>Known: 1</p>

Drawing on Sam's page

On Sam's page, render doesn't return anything: it sets the textContent of the parts of the page that show the deck. Call it once when the page opens, and again after every action, and the page always matches the deck.

Render after every change

const deck = { cards: ["Hi?", "Bye?"], index: 0 };

function render(d) {
  document.querySelector("#card").textContent = `Card ${d.index + 1}: ${d.cards[d.index]}`;
}

render(deck);
console.log(document.querySelector("#card").textContent);
deck.index = 1;
render(deck);
console.log(document.querySelector("#card").textContent);

Output

Card 1: Hi?
Card 2: Bye?

Tip

Use textContent instead of innerHTML for text typed by users. innerHTML runs any HTML it's given, which attackers can abuse.

Now try it yourself

This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.