Browse Source

19-quiz

19-quiz-api
emieldkr 4 years ago
parent
commit
54ab50afa7
2 changed files with 31 additions and 7 deletions
  1. 9
    0
      19-quiz/debug.log
  2. 22
    7
      19-quiz/script.js

+ 9
- 0
19-quiz/debug.log View File

@@ -16,3 +16,12 @@
[0113/114840.708:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/114840.708:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/114840.708:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/120340.709:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/120340.709:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/120340.709:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/181327.513:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/181327.514:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0113/181327.514:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0114/120007.772:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0114/120007.776:ERROR:crash_report_database_win.cc(469)] failed to stat report
[0114/120007.776:ERROR:crash_report_database_win.cc(469)] failed to stat report

+ 22
- 7
19-quiz/script.js View File

@@ -99,8 +99,7 @@ function answerClicked(event) {

function checkAnswer(answer) {
let questionIndex = question.getAttribute('data-index');
console.log(questions[questionIndex].correctAnswer);
if (questions[questionIndex].correctAnswer == answer) {
if (questions[questionIndex].correct_answer == answer) {
resultContainer.children[questionIndex].classList.replace(
'is-dark',
'is-success'
@@ -120,7 +119,7 @@ function addTotalScore() {

function init() {
question.setAttribute('data-index', 0);
randomizeQuestions();
// randomizeQuestions();
setNewQuestion();
}

@@ -128,15 +127,16 @@ function setNewQuestion() {
index = question.getAttribute('data-index');
if (index === '10') {
console.log('new game now');
question.innerText = `Your score was ${totalCorrect.innerText}, would you like to play again?`;
question.innerHTML = `Your score was ${totalCorrect.innerText}, would you like to play again?`;
gameOver = true;
} else {
question.innerText = questions[index].question;
console.log(questions);
question.innerHTML = questions[index].question;
}
}

function resetGame() {
init();
fetchFromAPI();
let resultArray = Array.from(resultContainer.children);
resultArray.forEach(function(result) {
result.classList.replace('is-danger', 'is-dark');
@@ -146,4 +146,19 @@ function resetGame() {
totalCorrect.innerText = 0;
}

init();
function fetchFromAPI() {
fetch(
'https://opentdb.com/api.php?amount=10&category=15&difficulty=medium&type=boolean'
)
.then(response => {
return response.json();
})
.then(myJson => {
questions = myJson.results;
})
.then(response => {
init();
});
}

fetchFromAPI();

Loading…
Cancel
Save