FlashCard Quiz App

Tell us what’s happening:

I am currently having issues with requirements 8, 9, and 12. It seems like deleting something from the current cards seems to be working. I’m not sure what I;m missing.

As for requirement 12. It seems that the InvalidUserInputError object doesn’t exist in the Error class.

Do I need to make a customary Error Class that extends Error?

Your code so far

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <meta content="width=device-width, initial-scale=1.0"/>
    <link rel="stylesheet" href="./styles.css"/>
  </head>
  <body>
    <div id="app">
      <h1 id="title">FlashCard App</h1>
      <p id="description">Click to flip the card and test your knowledge.</p>
      <div id="flashcard"></div>
      <button type="button" id="delete-btn">Delete Card</button>
      <form id="entry-form">
        <p id="form-description">Enter the question in the front and the answer in the back.</p>
        <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="entry-form-submit">Add Card</button>
      </form>
    </div>
    <footer id="watermark"></footer>
    <script src="./index.ts"></script>
  </body>
</html>

CSS

/* CSS RESET*/
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body{
  font-family: "Roboto Mono", system-ui;
}

#app{
  width: 70%;
  max-width: 760px;
  min-width: 360px;
  margin: auto;
  padding: 20px;
  border: 2px solid black;
}

#title{
  text-align: center;
  margin-bottom: 20px;
}
#description{
  text-align: center;
  margin-block: 10px;
}

#flashcard{
  max-width: 400px;
  width: 90%;
  min-width: 320px;
  height: 300px;
  background-color: gray;
  position: relative;
  
  margin: auto;
}

#flashcard p{
  height: 100%;
  font-size: 2rem;
  font-weight: 700;
  display: flex;
  text-align: center;
  justify-content: center;
  align-items: center;
}


button{
  display: block
}

#app label, #app textarea{
  display: block;
}
#app textarea{
  width: 100%;
}
#delete-btn{
  margin: auto;
  font-size: 1rem;
  margin-block: 10px;
}

TS

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

let currentCards: FlashCard[];
let currentCard: FlashCard;
let currentIndex: number;
const flashCard = document.querySelector("#flashcard") as HTMLElement;

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

currentCards = [
  {
    questionText: "What is the field of science that deals with interactions with matter and chemicals?",
    questionAnswer: "Chemistry" 
  }
];
currentIndex = currentCards.length - 1;
currentCard = currentCards[currentIndex] || null;
flashCard.addEventListener("click", () => {
  
  if (flashCard.classList.contains("flipped")){
    flashCard.classList.remove("flipped");
    flashCard.innerHTML = `
      <p id="question-text">${currentCard ? currentCard.questionText : ""}</p>
    `;
  } else {
    flashCard.classList.add("flipped");
    flashCard.innerHTML = `
      <p id="question-answer">${currentCards.length > 0 ? currentCards[currentIndex].questionAnswer : ""}</p>
    `
  }
  
})

deleteBtn.addEventListener("click", () => {
  currentCards.pop();
  currentIndex--;
  currentCard = currentCards[currentIndex] || null;
  flashCard.classList.remove("flipped");
  flashCard.innerHTML = `
    <p id="question-text">${currentCard ? currentCard.questionText : ""}</p>
  `
  console.log("CURRENT AMOUNT OF CARDS:", currentCards.length);
})

const entryFormSubmitBtn = document.querySelector("#entry-form-submit") as HTMLButtonElement;
const form = document.getElementById("entry-form") as HTMLFormElement;
const frontText = document.getElementById("front-text") as HTMLInputElement;
const backText = document.getElementById("back-text") as HTMLInputElement;
entryFormSubmitBtn.addEventListener("click", (e: Event) => {
    e.preventDefault();
    if (!frontText.value || !backText.value){
      alert("You must have both front and back text inputs filled.");
    }
})

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

Hi @CasualWanderer20XX

You need to build out the functionality for the add button.

Yes, do that.

Happy coding

I just did that…it still didn’t like my code.

deleteBtn.addEventListener("click", () => {
  currentCards.pop();
  currentIndex = currentCards.length - 1;
  currentCard = currentCards[currentIndex] || null;
  flashCard.classList.remove("flipped");
  flashCard.innerHTML = `<p id="question-text">${currentCard ? currentCard.questionText : ""}</p>`;
  
})

My Bad. I was thinking of Requirement 12, but I ended up showing code meant for Requirements 8,9.

This is the code I was referring to for #12.

class InvalidUserInputError extends Error{
  constructor(message: string){
    super(message);
    this.name = "InvalidInputUserError";
  }
}
addCardBtn.addEventListener("click", (e: Event) => {
    e.preventDefault();
    try{
      if (frontText.value == "" || backText.value == ""){
        throw new InvalidUserInputError("You must have both front and back text inputs filled.");
      }
      const newCard: FlashCard = {
        questionText: frontText.value,
        questionAnswer: backText.value
      };
      currentCards.push(newCard);
      loadFlashCard();
    } catch (err){
      console.error(err);
    }
})

Hi @CasualWanderer20XX

Try updating the #flashcard element whenever someone adds a new card.

Happy coding

Actually, I already did that.

function loadFlashCard(){
  currentIndex = currentCards.length - 1 || 0;
  currentCard = currentCards[currentIndex] || null;

  if (flashCard &&flashCard.classList.contains("flipped")){
    flashCard.innerHTML = `
      <p id="question- text">${currentCard ? currentCard.questionAnswer : ""}</p>
    `;
  } else {
    if (flashCard)
      flashCard.innerHTML = `
      <p id="question-answer">${currentCards.length > 0 ? currentCards[currentIndex].questionText : ""}</p>
    `
  }
}