3

I´m making a quiz with javascript and I have encountered a problem with few things. The quiz is works so that the user clicks the checkbox that has the correct answer.

My HTML form looks like this, so the correct answer is checked here in html part:

<div id="kysymys"> <p id="kymysys"> </p> </div>
    <div id="answerit"></div>
    <div id="vastausformi">
        <form id="vastaukset"> 
            <label for="a">A)</label>
            <input type="radio" classname="box" name="box" id="check1" value="a" onclick="checkGuess(this.value)" /><br>
    
            <label for="b">B)</label>
            <input type="radio" classname="box" name="box" id="check2" value="b" onclick="checkGuess(this.value)" /><br>
             
            <label for="c">C)</label>
            <input type="radio" classname="box" name="box" id="check3" value="c" onclick="checkGuess(this.value)" /><br>
             
            <label for="d">D)</label>
            <input type="radio" classname="box" name="box" id="check4" value="d" onclick="checkGuess(this.value)" />
        </form>
    </div>

So in my JS I´m trying to get the questions and the answers to be displayed on html. My javascript looks like this:

const questions= {
  
  question: "Which city is the capital of Sweden?",
  vastaus:{
      a: 'Stockholm',
      b: 'Malmö',
      c: 'Göteborg',
      d: 'Helsingborg'
  },
  correctAnswer: 'a',

  
  question: "Which animal can be seen on the Porsche logo?",
  vastaus:{
      a:'Buffalo',
      b:'Horse',
      c:'Jaguar',
      d:'Deer'
  },
  correctAnswer:'b'
};



function showAnswers(){
  for (const vastaus in questions) {
  document.getElementById("answerit").innerHTML=`${vastaus}: ${questions.vastaus}`.toString();
}}
showAnswers();


function showQuestion(){
  
  for (const question in questions) {
    document.getElementById("kymysys").innerHTML = `${question}: ${questions[question]}`.toString();
    
}
}
  showQuestion();

For some reason, the part that gets displayed is only the "correctAnswer:'b'" part and nothing else. I also have problem with "displaying only one question", so it always shows the last/latest question added to javascript.

I have tried to add a compeletely new div-element to html that would hold the answers, but it doesn´t work, it only shows the 'correctAnswer: b'- part. I have tried to use loops so that it would go through the questions one at a time so that it would only show the one that I want, but it didn´t work so I took it out, since I can´t get those things to display properly even without it.

Also for the other problem, displaying only question at a time and going to the other question after clicking a button, I have tried loops to go through the questions, but with no luck. This second problem (displaying one question etc.) is bigger so I will probably just do other post with better description of it, but if anyone happens to know a way with only this, I would be thankfullness.

I´m sorry for my english, but hopefully you will understand what I mean. If anyone knows how fix this I would be greatful. I have little experience with coding so things I do might not be correct. If you need more describing I will try.

6
  • Should not ${vastaus}: ${questions.vastaus} be ${vastaus}: ${questions[vastaus]}? Commented Dec 17, 2022 at 7:57
  • Also, the for loop in showAnswers is overwritng innerHTML at each interation rather than appending an element to the DOM. Commented Dec 17, 2022 at 8:01
  • @Gary Yeah I forgot to put it back, I tried different ways to get it to display properly, so that was previously correct. Thank you anyways. Commented Dec 17, 2022 at 8:02
  • @DreamBold Okay, then I have to think something else to do. Thank you for answer. Commented Dec 17, 2022 at 8:03
  • 1
    I have updated the answer a little, hope it helps! Commented Dec 18, 2022 at 6:34

1 Answer 1

1

You can try the code below, it shows all the questions and answers as it's a demo for now, but you can improve it as you need, you are good to move forward!

const questions = [
  {
    question: "Which city is the capital of Sweden?",
    vastaus: {
      a: "Stockholm",
      b: "Malmö",
      c: "Göteborg",
      d: "Helsingborg"
    },
    correctAnswer: "a"
  },

  {
    question: "Which animal can be seen on the Porsche logo?",
    vastaus: {
      a: "Buffalo",
      b: "Horse",
      c: "Jaguar",
      d: "Deer"
    },
    correctAnswer: "b"
  }
];
let index = 0;
function showAnswer(index) {
  if(index < questions.length && index >= 0){
    document.getElementsByClassName("answerit")[0].innerHTML =
      questions[index].question;
    document.getElementsByTagName("label")[0].textContent =
      "A)  " + questions[index].vastaus.a;
    document.getElementsByTagName("label")[1].textContent =
      "B)  " + questions[index].vastaus.b;
    document.getElementsByTagName("label")[2].textContent =
      "C)  " + questions[index].vastaus.c;
    document.getElementsByTagName("label")[3].textContent =
      "D)  " + questions[index].vastaus.d;
   }else{
    alert("The end!");
   }
}

showAnswer(index);

document.getElementById("next").addEventListener("click", function () {
  showAnswer(++index);
});

document.getElementById("prev").addEventListener("click", function () {
  showAnswer(--index);
});
<div id="kysymys">
  <p id="kymysys"> </p>
</div>
<div class="answerit"></div>
<div id="vastausformi">
  <form id="vastaukset">
    <label for="a">A)</label>
    <input type="radio" classname="box" name="box" id="check1" value="a" onclick="checkGuess(this.value)" /><br>

    <label for="b">B)</label>
    <input type="radio" classname="box" name="box" id="check2" value="b" onclick="checkGuess(this.value)" /><br>

    <label for="c">C)</label>
    <input type="radio" classname="box" name="box" id="check3" value="c" onclick="checkGuess(this.value)" /><br>

    <label for="d">D)</label>
    <input type="radio" classname="box" name="box" id="check4" value="d" onclick="checkGuess(this.value)" />
    <br />
    <input type="button" id="prev" value="Prev Question">
    <input type="button" id="next" value="Next Question">
  </form>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.