jeudi 23 janvier 2020

Using If Statement To Call A JSON File

I have a quiz saved in a question.json which ought to be a different question based on the selected course. I tried using an if statement. where courses is the select field in the form.

      <div id="home" class="flex-center flex-column">
        <h1>Quick Quiz</h1>
        <select id="ddlViewBy" onChange="check()" >
          <option >Select Value</option>
          <option value="1">test1</option>
          <option value="2">test2</option>
          <option value="3">test3</option>
        </select>
        <a class="btn" href="/game.html">Play</a>
        <a class="btn" href="/highscores.html">High Scores</a>
      </div>
    </div>




function check() {
   val = document.getElementById('courses').value;

 }

 var val = check();

 if(val == 1) {
  fetch("questions.json")
.then(res => {
  return res.json();
})
.then(loadedQuestions => {
  console.log(loadedQuestions);
  questions = loadedQuestions;
  startGame();
})
.catch(err => {
  console.error(err);
});
console.log('How are you');
} else {
 console.log('not working');
}

I want to load the question if val == 1 and load another question if val == 2 and so on.

I'm open to new ways to achieve this

Aucun commentaire:

Enregistrer un commentaire