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 User Interface Code

Anthony c
Anthony c
20,907 Points

onlick is not registering

My quiz is rending the first question with no errors. However, my onlick is not working. It is saying it's null.

So, for some reason it is not getting fed the correct id. And I honestly have no idea how guessHandler is being passed the id argument. I know it comes from displayChoices but I can't figure out why it's not working.

index.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Amazing Quiz</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div class="grid">
    <div id="quiz" class="centered grid__col--8">
        <h1>Awesome Quiz</h1>

        <h2 id="question" class="headline-secondary--grouped"></h2>

        <p id="choice0"></p>
        <button id="guess0" class="btn--default">Select Answer</button>

        <p id="choice1"></p>
        <button id="guess1" class="btn--default">Select Answer</button>

        <footer>
            <p id="progress">Question x of y</p>
        </footer>
    </div>
</div>

<script src="quiz.js"></script>
<script src="question.js"></script>
<script src="quiz_ui.js"></script>
<script src="app.js"></script>
</body>
</html>

app.js

var questions = [
  new Question("Who was the first President of the United States?",["George Washington","Thomas Jeffereson"],"George Washington"),
  new Question("What is it?",["Pi","42"],"42")
];


var quiz = new Quiz(questions);


QuizUI.displayNext();

question.js

function Question(text, choices, answer) {
  this.text = text;
  this.choices = choices;
  this.answer = answer;
}


Question.prototype.isCorrectAnswer = function(choice) {
    return this.answer === choice;
}

quiz.js

function Quiz(questions) {
 this.score = 0;
 this.questions = questions;
 this.currentQuestionIndex = 0;
}


Quiz.prototype.guess = function(answer) {
  if(this.getCurrentQuestion().isCorrectAnswer(answer)) {
    this.score++;
  }  
  this.currentQuestionIndex++
}


Quiz.prototype.getCurrentQuestion = function() {
  return this.questions[this.currentQuestionIndex];
};


Quiz.prototype.hasEnded = function() {
  return this.currentQuestionIndex >= this.questions.length;
};

quiz_ui.js

var QuizUI = {
    displayNext: function() {
        if(quiz.hasEnded()) {
          this.displayScore();
        } else {    
          this.displayQuestion();
          this.displayChoices();
          this.displayProgress();
        }
  },
  displayQuestion: function() {
    this.populateIdWithHTML("question", quiz.getCurrentQuestion().text);
  },
  displayChoices: function() {
    var choices = quiz.getCurrentQuestion().choices;

    for(var i = 0; i < choices.length; i++) {
      this.populateIdWithHTML("choice" + i, choices[i]);
      this.guessHandler("guess" + i, choices[i]);
    }
  },
  displayScore: function() {
    var gameOverHTML = "<h1>Game Over</h1>";
    gameOverHTML += "<h2> Your score is: " + quiz.score + "</h2>";
    this.populateIdWithHTML("quiz", gameOverHTML);

  },
  populateIdWithHTML: function(id, text) {
    var element = document.getElementById(id);
    element.innerHTML = text;
  },
  guessHandler: function(id, guess) {
    var button = document.getElementById(id);
     button.onlick = function(){
       quiz.guess(guess);
       QuizUI.displayNext();
     }
  },

  displayProgress: function() {
    var currenQuestionNumber = quiz.currentQuestionIndex + 1;
    this.populateIdWithHTML("progress", "Question " + currenQuestionNumber + " of " + quiz.questions.length);
  }
};

2 Answers

Gavin Ralston
Gavin Ralston
28,770 Points

Any chance you meant to type onclick? :)

button.onlick = function(){
       quiz.guess(guess);
       QuizUI.displayNext();
Anthony c
Anthony c
20,907 Points

OMG I ALWAYS DO ONLICK. I did this on the todo list interactive website course