Build a FlashCard App Project

Tell us what’s happening:

So, I rebuilt my project from the ground up. Similar, but different code. It’s still not complete, but here’s what I got so far.

All tests except for 8, 9, and 12 pass…so the same place as I was before.

Your code so far

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="./styles.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Pen</title>
  </head>
  <body>
    <div class="container" id="flashcard-app">
      <h1>FlashCard App Project</h1>
      <div class="container" id="card-interface">
        <div id="flashcard">
        </div>
      </div>
      <div class="container" id="menu">
        <section class="section" id="manage">
          <p>Manage Cards</p>
          <button type="button" id="delete-btn">Delete Current Card</button>
        </section>
        <section class="section" id="card-list">
          

        </section>
        <section class="section" id="add-card-section">
          <form id="entry-form">
            <label for="front-text">Question</label>
            <textarea id="front-text"></textarea>
            <label for="back-text">Answer</label>
            <textarea id="back-text"></textarea>
            <button type=button id="submit-btn">Add Card</button>
          </form>
        </section>
      </div>
    </div>
    <script src="./index.ts"></script>
  </body>
</html>

CSS

html {
  height: 100%;
  background: 50% 50% no-repeat
    url('data:image/svg+xml,<svg viewBox="-20 -5 60 40" width="140" xmlns="http://www.w3.org/2000/svg" opacity="0.2"><path d="M1 8l11-7 11 7v8l-11 7-11-7zm0 0l11 8 11-8m0 8L12 8 1 16M12 1v7m0 8v7" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="2"/><text x="-12" y="32" font-size="5" font-family="monospace" fill="currentColor">Built on CodePen</text></svg>');
}

/* CSS RESET */
*{
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body{
  background: #202;
  color: white;
  font-family: "Roboto Mono", system-ui;
}
.container{
  border: 2px solid black;
  
}
#flashcard-app{
  margin: auto;
  width: 75%;
  max-width: 860px;
  min-width: 360px;
  background: #444;
  padding: 20px;
}
#flashcard-app > h1{
  text-align: center;
}

#flashcard-app .container{
  padding: 20px;
}

#card-interface{
  background: black;
}
#flashcard{
  background: white;
  color: black;
  border-radius: 20px;
  padding: 20px;
  width: calc(100% - 20px);
  aspect-ratio: 16/9;
  margin: auto;
}

#flashcard > p {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}

Note: I made this project on Codepen, so the watermark CSS was kept here.

TypeScript

interface FlashCard{
  questionText: string;
  questionAnswer: string;
}

let currentCards: FlashCard[];
let currentCard: FlashCard;

currentCards = [{
  questionText: "First month of the year.",
  questionAnswer: "January"
}]

const flashCard = document.querySelector("#flashcard") as HTMLElement;

const nullCard = {
  questionText: "",
  questionAnswer: ""
}

const deleteBtn = document.querySelector<HTMLButtonElement>("#delete-btn");


function renderFlashCard(card: FlashCard = currentCards[currentCards.length - 1] || nullCard): string{
  console.log("Card:", card);
  currentCard = card;
  console.log(currentCard);
  let HTMLString = ``;
  const isFrontSide: boolean = flashCard.classList.contains("flipped");
  const cardIsntNull = card !== nullCard;
  console.log(cardIsntNull);
  if (!isFrontSide)
    HTMLString = `<p id="front-text">${cardIsntNull ? card.questionText : ""}</p>`
  else
    HTMLString = `<p id="back-text">${cardIsntNull ? card.questionAnswer : ""}</p>`
  return HTMLString;
}

document.addEventListener("DOMContentLoaded", () => {
  flashCard.innerHTML = renderFlashCard();
})

flashCard.addEventListener("click", () => {
  flashCard.classList.toggle("flipped");
  flashCard.innerHTML = renderFlashCard();
})

if (deleteBtn)
  deleteBtn.addEventListener("click", () => {
    currentCards = currentCards.filter(card => card != currentCard);
    flashCard.innerHTML = renderFlashCard();
  })

Lesson URL (copy - paste from your browser’s address bar)

I do have a couple questions regarding these tests.

Questions

Tests 8 and 9.

  1. Do the tests expect the flashCard[] array to filter out the element that was currently on display?
  2. If so, does it expect a cache or data of the previous card displayed to be stored so that once the current card is deleted, that previous card is displayed instead?

Test 12

  1. I didn’t make the code to cover this for this version of the project yet, but when I did this previously, I made a custom class for the InvalidUserInputError to be thrown when either input is empty. The test still failed. Was there something specific the test wanted from this class?

Update: So, I typed the code for Test 12 and…uh…it worked (YEEEEESSSSS).

Turns out, instead of giving the submit button the event listener (Making the button a type=“button” instead of type=“submit”) and having the card be created from it along with the error to be thrown, I needed to apply the event listener to the FORM.

I had to make the form’s event listener to “submit”, prevent the default event, then apply the card creation + error throwing features there.

:stuck_out_tongue:

This is all the code added after the TS code above.

// Form Stuff

const entryForm = document.querySelector("#entry-form") as HTMLFormElement;


class InvalidUserInputError extends Error{
  constructor(message: string){
    super(message);
    this.name = "InvalidUserInputError"
  }
}

entryForm.addEventListener("submit", (e: Event) => {
  e.preventDefault();
  const frontText = document.querySelector("#front-text") as HTMLInputElement;
  const backText = document.querySelector("#back-text") as HTMLInputElement;

  try {
    if (frontText.value && backText.value){
      const newCard: FlashCard = {
        questionText: frontText.value,
        questionAnswer: backText.value
      }
      currentCards.push(newCard);
    } else {
      throw new InvalidUserInputError("Either input is empty.")
    }
  }
  catch (err){
    console.error(err);
  }
})

Oh yeah and Test 8 passed.