Tell us what’s happening:
Hi! I can’t pass the 9th test although it’s functional. Sadly other posts about this issue didn’t help me…
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML 5 Boilerplate</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class='container outer-container'>
<h2 class='container'>Flashcard Quizz</h2>
<div class='container flashcard-container'>
<div id='flashcard' class='container'>
</div>
</div>
<div class='container outer-container' id='manage-cards'>
<div id='add' class='container card-managers'>
<h3>Add cards</h3>
<form id='entry-form'>
<label for='front-text'> Front text:
<textarea id='front-text' placeholder='How many stars are there in the solar system?'></textarea>
</label>
<label for='back-text'> Back text:
<textarea id='back-text' placeholder='just One!'></textarea>
</label>
<button id='add-card-btn' type='submit'>Add Card</button>
</form>
</div>
<div class='container card-managers'>
<h3>All cards</h3>
<button id='delete-btn' type='submit'>Delete current Card</button>
<div id='all-cards'><div>
</div>
</div>
</div>
<script src="index.ts"></script>
</body>
</html>
/* file: index.ts */
interface FlashCard {
questionText: string;
questionAnswer: string;
}
let currentCards: FlashCard[] = [];
const firstCard = {questionText: "How many stars are there in the solar system?", questionAnswer: "Just one!"};
const secondCard = {questionText: "How many fingers do a human have?", questionAnswer: "20"};
currentCards.push(firstCard)
currentCards.push(secondCard)
const getElement = (id: string): HTMLElement | HTMLInputElement => {
const el = document.getElementById(id);
if (!el) {
throw new Error(`Element not found: ${id}`)
}
return el
}
/*Getting elements*/
const flashCard = getElement('flashcard') as HTMLElement;
const allCards = getElement('all-cards') as HTMLElement;
const frontText = getElement('front-text') as HTMLInputElement;
const backText = getElement('back-text') as HTMLInputElement;
const form = getElement('entry-form');
const deleteBtn = getElement('delete-btn');
/*Variables*/
let curr = 0;
/*Error*/
class InvalidUserInputError extends Error {
constructor(message: string) {
super(message);
this.name = 'InvalidUserInputString';
}
}
/*Logic*/
const flipFlashCard = () => {
flashCard.classList.toggle('flipped');
}
const updateFlashCard = (card: FlashCard) => {
if (!currentCards.length) {
flashCard.textContent = 'Please add a card!';
return
}
flashCard.textContent = !flashCard.classList.contains('flipped') ? card?.questionText : card?.questionAnswer;
}
const generateCard = (card: FlashCard, i: number, current: boolean) => {
return `
<div class='flex-row ${current ? "current" : ""}' onClick='handleClick(${i})' style='cursor: pointer'>
<div>${card.questionText}</div>
<div>${card.questionAnswer}</div>
</div>
`
}
const displayCards = () => {
allCards.innerHTML = '';
let i = 0;
currentCards.forEach((card, i) => {
const cardHTML = generateCard(card, i, i === curr);
allCards.innerHTML += cardHTML;
i++;
})
}
const validateForm = (front: string, back: string) => {
if (front === '' || back === '') {
throw new InvalidUserInputError('')
}
}
const clearForm = (...inputs: HTMLInputElement[]) => {
inputs.forEach((input) => {
input.value = '';
})
}
const addCard = (card: FlashCard) => {
currentCards.push(card);
}
const handleSubmit = () => {
console.log('submitting form...')
const front = frontText.value;
const back = backText.value;
validateForm(front, back);
const newCard: FlashCard = {
questionText: front,
questionAnswer: back
}
addCard(newCard)
clearForm(frontText, backText);
//console.log(currentCards);
//console.log("Form submitted!");
//console.log("Updating display...");
displayCards();
//console.log('Display updated!')
}
const handleClick = (i: number): void => {
curr = i;
console.log(curr);
flashCard.classList.remove('flipped');
displayCards();
updateFlashCard(currentCards[curr]);
}
const deleteCurrent = (): void => {
if (currentCards.length === 0) {
return
} else {
currentCards.splice(curr, 1)
if (curr !== 0) {
curr--
}
flashCard.classList.remove('flipped');
displayCards();
updateFlashCard(currentCards[curr]);
}
}
/*Event listeners*/
flashCard.addEventListener('click', () => {
flipFlashCard();
updateFlashCard(currentCards[curr]);
})
document.addEventListener('DOMContentLoaded', () => {
updateFlashCard(currentCards[curr]);
displayCards()
})
form.addEventListener('submit', handleSubmit);
deleteBtn.addEventListener('click', deleteCurrent)
/* file: styles.css */
h2 {
background: linear-gradient(90deg, #111, #212121, #111);
color: #eee;
text-align: center;
padding: 30px 10px;
margin: 0;
}
.container {
padding: 20px;
border: 2px solid #212121;
}
#flashcard {
text-align: center;
background-color: #212121;
color: #eee;
transition: all 1s ease;
}
.outer-container {
display: flex;
flex-direction: column;
gap: 10px;
}
.card-managers {
text-align: center;
padding: 10px;
display: flex;
flex-direction: column;
}
#entry-form {
display: flex;
flex-direction: column;
gap: 5px;
}
#entry-form textarea {
width: 70%;
}
#entry-form label {
display: flex;
justify-content: space-evenly;
align-items: center;
}
#add-card-btn {
background-color: #418721;
color: #eee;
padding: 8px;
border: none;
width: 30%;
margin: 10px auto;
transition: all 0.2s ease;
}
#add-card-btn:hover {
background-color: #216721;
cursor: pointer;
}
.flipped {
background-color: #eee !important;
color: #212121 !important;
}
.flex-row {
display: flex;
border: 1px solid black;
justify-content: space-evenly;
align-items: center;
}
#delete-btn {
background-color: #e11;
color: white;
padding: 5px;
border: none;
transition: all 0.5s ease
}
#delete-btn:hover {
background-color: #990909;
cursor: pointer;
}
#all-cards {
display: flex;
flex-direction: column;
gap: 8px;
}
#delete-btn {
margin-bottom: 8px;
}
.current {
background-color: #f55;
transition: all 0.5 ease;
}
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:157.0) Gecko/20100101 Firefox/157.0
Challenge Information:
Build a Flashcard Quiz App - Build a Flashcard Quiz App