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 Object-Oriented JavaScript (2015) Practice Project Project Overview

Vlad Legkowski
Vlad Legkowski
9,882 Points

Functional solution

Hate to admit but OOP is not my strongest side, so for all of those who are in the same boat...

function Question(text, choices, answer) {
    this.text = text;
    this.choices = choices;
    this.answer = answer;
}
let j = 0;
let score = 0;
function createQuestion () {
  if (j === questions.length) {
    document.querySelector('#quiz').innerHTML = `You answered ${score} questions correctly out of ${questions.length}`;
  } else {
    document.querySelector('#progress').innerHTML = `Question ${j+1} out of ${questions.length}`;
    document.querySelector('#question').innerHTML = questions[j].text;
    document.querySelector('#choice0').innerHTML = questions[j].choices[0];
    document.querySelector('#choice1').innerHTML = questions[j].choices[1];
  }
}
document.addEventListener('click', e => {
  if (e.target.tagName !== 'BUTTON') { return; }
  if (e.target.tagName === 'BUTTON' && e.target.previousElementSibling.textContent === questions[j].answer) { score++; }
  j++;
  createQuestion(questions);
});
createQuestion(questions);