Tell us what’s happening:
Test number 9. When the delete-btn is clicked, the previous flashcard data should be displayed. Doesn’t pass. I tried changing the delete function multiple times and even looking at what the test does but still doesn’t pass. Please help. In the preview the delete function works correctly.
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" />
<title>Flashcard Quiz App</title>
<link href="styles.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Quiz Card</h1>
<div id="flashcard">
<div class="cardFace frontFace"></div>
<div class="cardFace backFace"></div>
</div>
</div>
</div>
<div class="container">
<h2>Manage Card<h2>
<button class="button" id="delete-btn">Delete Card</button>
<hr>
<h2>All Cards</h2>
<div id="card-container"></div>
<hr>
<h2>Add Card</h2>
<form id="entry-form">
<label class="label" for="front-text">Front text:</label>
<textarea class="inputCard" id="front-text"></textarea>
<label class="label" for="back-text">Back text:</label>
<textarea id="back-text" class="inputCard"></textarea>
<div id="alert" class="hidden">⚠ Front text cannot be empty.</div>
<button id="save-button"class="button"
type="submit">Save Card</button>
</form>
</div>
<script src="index.ts"></script>
</body>
</html>
/* file: index.ts */
interface FlashCard {
id:number;
questionText:string;
questionAnswer:string;
}
class InvalidUserInputError extends Error{
public msg:string;
constructor(msg:string){
super();
this.msg = msg;
}
}
const firstFlashCard ={
id:0,
questionText:"What is the color of the white horse",
questionAnswer:"White",
}
const secondFlashCard ={
id:1,
questionText:"What is the color of the black horse",
questionAnswer:"White",
}
const currentCards:FlashCard[] = [firstFlashCard,secondFlashCard];
let counterId:number = 1;
let deleteBtn:HTMLElement;
let frontIntput:HTMLInputElement;
let backInput:HTMLInputElement;
let submitBtn:HTMLElement;
let cardContainer:HTMLElement;
let flashCard:HTMLElement;
let front:HTMLElement;
let back:HTMLElement;
let previousSelected:HTMLElement;
let alertMessage: HTMLElement
let currentCardIndex:number;
function toggleFlipped(){
flashCard.classList.toggle("flipped");
}
function renderCard(card:FlashCard){
const cardButton = document.createElement("button");
cardButton.classList.add("card");
cardButton.id = `${card.id}`
cardButton.innerText = card.questionText.length > 20? card.questionText.slice(0,20) +"...": card.questionText;
return cardButton;
}
function selectCard(e:Event){
const target = e.target as HTMLElement;
if(!target){
return
}
if(previousSelected){
previousSelected.classList.remove("selected");
}
target.classList.add("selected");
previousSelected = target;
const id = Number(target.id);
currentCardIndex = currentCards.findIndex(card=> card.id === id);
if(!currentCardIndex){
currentCardIndex = 0;
}
changeCard(currentCards[currentCardIndex].questionText,currentCards[currentCardIndex].questionAnswer);
}
function addCards(){
cardContainer.innerHTML = "";
currentCards.forEach(card=>{
const element = renderCard(card);
element.addEventListener("click",selectCard);
cardContainer.appendChild(element);
});
}
function deleteCard(){
currentCards.splice(currentCardIndex,1);
}
function initializeCards(){
if(currentCards.length === 0 ){
addCards();
return;
}
else{
firstCard();
addCards();
}
}
function firstCard(){
addCards();
cardContainer.children[0].classList.add("selected");
currentCardIndex = 0;
changeCard(currentCards[currentCardIndex].questionText,currentCards[currentCardIndex].questionAnswer);
previousSelected = cardContainer.children[0] as HTMLElement;
}
function changeCard(frontS:string,backS:string){
front.textContent = `${frontS}`;
back.textContent = `${backS}`;
}
function saveCard(e:Event){
try{
e.preventDefault();
if(!frontIntput.value ||!backInput.value){
throw new InvalidUserInputError("Inputs must contain messages");
}
alertMessage.classList.add("hidden");
counterId++
currentCards.push({id:counterId ,questionText:frontIntput.value,questionAnswer:backInput.value});
if(currentCards.length === 1){
firstCard();
}else{
addCards();
}
changeCard(frontIntput.value,backInput.value)
currentCardIndex = currentCards.length - 1;
frontIntput.value = "" ;
backInput.value = "";
}
catch(error){
alertMessage.classList.remove("hidden");
console.error(error)
}
}
const displayCard = (index: number): void => {
const card: FlashCard = currentCards.length > 0 ? currentCards[index] : {id:-1, questionText: "", questionAnswer: "" };
changeCard(card.questionText,card.questionAnswer);
}
const initializeApp = () => {
deleteBtn = document.getElementById("delete-btn") as HTMLElement;
frontIntput = document.getElementById("front-text") as HTMLInputElement;
backInput = document.getElementById("back-text") as HTMLInputElement;
submitBtn = document.getElementById("save-button") as HTMLElement;
cardContainer = document.getElementById("card-container") as HTMLElement;
flashCard = document.querySelector("#flashcard") as HTMLElement;
alertMessage = document.getElementById("alert") as HTMLElement;
front = document.querySelector(".frontFace") as HTMLElement;
back = document.querySelector(".backFace") as HTMLElement;
initializeCards();
deleteBtn.addEventListener("click",()=>{
deleteCard();
displayCard(0);
initializeCards();
});
flashCard.addEventListener("click",toggleFlipped);
submitBtn.addEventListener("click",saveCard);
}
document.addEventListener("DOMContentLoaded", initializeApp);
/* file: styles.css */
*{
box-sizing: border-box;
}
body{
background-color:#3E3E4D;
}
hr{
width:100%;
}
h2{
text-align:left;
font-size:1.5rem;
}
.cardFace {
display: flex;
justify-content: center;
align-items: center;
color: white;
background-color: #3E3E4D;
width: 100%;
height: 100%;
border-radius: 10px;
backface-visibility: hidden;
-webkit-backface-visibility: hidden;
transition: transform .5s linear;
transform-style: preserve-3d;
}
#flashcard{
height: 30%;
width: 100%;
background-color: #3E3E4D;
}
.backFace {
background-color: #3E3E4D;
transform: rotateY(180deg) ;
}
.frontFace {
background-color: #3E3E4D;
transform: rotateY(0deg) ;
}
.flipped:hover .backFace {
transform: rotateY(360deg);
}
.flipped:hover .frontFace {
transform: rotateY(180deg);
}
.container{
display:flex;
flex-direction: column;
margin: 5% 10%;
padding: 5%;
justify-content: space-around;
background-color: #71718E;
border-radius: 20px;
text-align:center;
color:white;
font-weight:bold;
}
.button{
width:100%;
text-align:center;
background-color:#3535CC;
border:none;
color:white;
padding: 15px;
border-radius: 10px;
font-size:1.3rem;
transition: all 0.3s;
}
.button:hover{
background-color:#2A2AA7;
}
.button:active,.card:active,selected:active{
transform: scale(0.95);
}
#entry-form{
display:flex;
flex-direction: column;
gap: 15px;
text-align:left;
justify-content:space-around;
align-items:center;
}
.label{
text-align:left;
color:#D7D7DF;
}
.inputCard{
width:100%;
max-width:100%;
min-width:100%;
min-height:20px;
background-color:#202028;
border:none;
font-size:1.2rem;
padding:5px;
border-radius: 5px;
color:white;
}
#card-container{
display:flex;
flex-direction:column;
gap: 10px;
}
.card{
width:auto;
text-align:center;
background-color:#3535CC;
color:white;
padding: 10px;
border-radius: 10px;
font-size:1.2rem;
transition: all 0.3s;
cursor: pointer;
}
.card:hover{
background-color:#212183;
color:gray;
}
.selected{
background-color:#212183;
color:gray;
}
.hidden{
display:none;
}
#alert{
color:#FF7A7A;
font-size:1.1rem;
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36
Challenge Information:
Build a Flashcard Quiz App - Build a Flashcard Quiz App