}
if (level === 'hard') {
flash.innerHTML = 300
}
fetch(`https://opentdb.com/api.php?amount=1&category=$
{genre.id}&difficulty=${level}&type=boolean`) //i added the ${genre.id} and the ${level} so that the link is dynamic and changes for each category and difficulty level .then(response => response.json()) //this converts the json data into data that is usable for the program .then(data => { // This link comes from Open Trivia Database (https://opentdb.com/) where I was able to generate an API link with the constraints that I require for my task. The link personalizes the information so that I can recieve questions that fit the difficulty and genre that I choose. console.log(data) flash.setAttribute('data-question', data.results[0].question) flash.setAttribute('data-answer', data.results[0].correct_answer) flash.setAttribute('data-value', flash.getInnerHTML()) }) .then(done => flash.addEventListener('click', flashFlip)) //when the flash is clicked it runs the function flashFlip }) } genres.forEach(genre => genreAdd(genre)) function flashFlip() { this.innerHTML = '' this.style.fontSize = '15px' const textDisplay = document.createElement('div') const trueButton = document.createElement('button') // these three constants set the ids for the actual Questions, the true button, and the false button const falseButton = document.createElement('button') trueButton.innerHTML = 'True' //this is the text that shows up for the true and false buttons falseButton.innerHTML = 'False' trueButton.classList.add('true-button') //this adds the true button and the false button to a class falseButton.classList.add('false-button') trueButton.addEventListener('click', resultGet) //when you click each button it runs the resultGet function falseButton.addEventListener('click', resultGet) textDisplay.innerHTML = this.getAttribute('data-question') this.append(textDisplay, trueButton, falseButton) //this makes the questions and the two buttons show up on every flash const allCards = Array.from(document.querySelectorAll('.flash')) allCards.forEach(flash => flash.removeEventListener('click', flashFlip)) } function resultGet() { const flashsAll = Array.from(document.querySelectorAll('.flash')) flashsAll.forEach(flash => flash.addEventListener('click', flashFlip))
const flashButton = this.parentElement
if (flashButton.getAttribute('data-answer') === this.innerHTML) {
points = points + parseInt(flashButton.getAttribute('data-value'))
displayScore.innerHTML = points //the line above parses the points so
that it is converted to a string and then it is shown on the flash with innerHTML flashButton.classList.add('correct-answer') setTimeout(() => { while (flashButton.firstChild) { flashButton.removeChild(flashButton.lastChild) } flashButton.innerHTML = flashButton.getAttribute('data-value') }, 100) } else { flashButton.classList.add('wrong-answer') setTimeout(() => { while (flashButton.firstChild) { flashButton.removeChild(flashButton.lastChild) } flashButton.innerHTML = 0 }, 100) } flashButton.removeEventListener('click',flashFlip) } </script>
<style> body { marign: 0; padding: 0; background-color: #458c8c; color: #fff; font-family: 'Fantasy', Papyrus; display: flex; justify-content: center; align-items: center; flex-direction: column; } #trivia { display: flex; } .genre-column { display: flex; flex-direction: column; text-align: center; } .flash { width: 200px; height: 120px; border-radius: 20px; background-color: #66ab63; border: solid #6a3680 5px; text-align: center; padding: 5px;
margin: 10px;
font-size: 100px;
} .correct-answer { background-color: #07cc00; } .wrong-answer { background-color: #c73b1e; } .true-button { background-color: #63b36e; } .false-button { background-color: #aa231e; } button { margin: 10px; } </style>