Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Alex L
Alex L
5,024 Points

JavaScript Quiz, next question issue.

Hello,

I am trying to create a quiz to improve my JavaScript skills but I have got stuck on a certain part. On line 122 of my programme, I want the programme to clear the current question and answers then run the function again to create a new one when the next button is pressed.

https://codepen.io/xfvelocity/pen/JzVJVP

1 Answer

Steven Parker
Steven Parker
230,274 Points

Just implementing the steps you described (plus clearing the contents of the elements that will be re-used):

    } else if (buttonPress.className === "nextButton") {
      body.removeChild(questionDiv);                             // remove the old question
      questionDiv.innerHTML = "";                                // clear the contents
      ul.innerHTML = "";                                         // also clear the answer list
      createQuiz(Math.floor(Math.random() * questions.length));  // run the function again
    } else {  //...

You might want to select questions sequentially instead of randomly, as it's possible right now to get the same question twice in a row.

Alex L
Alex L
5,024 Points

Hi, thank you very much for your help. Didn't realize it'd be as simple as that.

I am aware of the duplicate questions issue and was implementing a way to prevent getting the same questions but wanted to fix the button first.