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

The Quiz UI isn't displaying everything.

Can't get the Quiz exercise from the Object Oriented Javascript course to work.

The UI only displays "Awesome Quiz" and the Select Answer buttons. It's not displaying either quiz question and it's not showing the answer options above each button. Also at the bottom of the UI the progress line says "Question x of y" instead of updating "Question 1 of 2" etc.

I keep getting this error code in the console: app.js:3 Uncaught ReferenceError: Question is not defined(anonymous function) @ app.js:3

I did the exercise yesterday, then came back to it this morning to compare it to the Quiz_Solution file and corrected a couple of errors that I found. Other than that I don't see what I'm missing.

Here are the three files I edited following the Andrew:

app.js
//Create Questions
var questions = [
    new Question("Who was the first President of the United States?", [ "George Washington", "Thomas Jefferson" ], "George Washington"),
    new Question("What is the answer to the Ultimate Question of Life, the Universe, and Everything?", ["Pi","42"], "42")
];

//Create Quiz
var quiz = new Quiz(questions);

//Display Quiz
QuizUI.displayNext();
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.onclick = function() {
        quiz.guess(guess);
        QuizUI.displayNext();
    }
},

displayProgress: function() {
    var currentQuestionNumber = quiz.currentQuestionIndex + 1;
    this.populateIdWithHTML("progress", "Question" + currentQuestionNumber + " of " + quiz.questions.length);
    }
};
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;
};

This is the question.js file.

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;
};
Sean T. Unwin
Sean T. Unwin
28,690 Points

I edited your posts so the code is easily readable. :)

8 Answers

Thank you Sean. (thumbs up)

Sean T. Unwin
Sean T. Unwin
28,690 Points

It appears to me that question.js is not loaded before app.js, based upon the error given because it doesn't recognize what a new Question() is.

Here is the question.js file where each Question is created.

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

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

By the way, how do you make the code samples appear in the black background like that?

Sean T. Unwin
Sean T. Unwin
28,690 Points

What is the order of your .js files in the HTML?

Here's the index.html file with the script tags near the bottom.

<!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>
        <h3 id="score"></h3>

        <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>
Sean T. Unwin
Sean T. Unwin
28,690 Points

I saved your files and it works for me. Perhaps you have a cache issue? Double check your file names, as well, to ensure they correspond to the script sources in your HTML.

Turns out it was a cache issue. I cleared the deck of all the history and it works fine now. Thanks!