Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
221,293 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.