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 trialMartin Lindsey
11,273 PointsThe 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:
//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();
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);
}
};
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;
};
Sean T. Unwin
28,690 PointsI edited your posts so the code is easily readable. :)
8 Answers
Martin Lindsey
11,273 PointsThank you Sean. (thumbs up)
Sean T. Unwin
28,690 PointsIt 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.
Martin Lindsey
11,273 PointsHere 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; };
Martin Lindsey
11,273 PointsBy the way, how do you make the code samples appear in the black background like that?
Sean T. Unwin
28,690 PointsPlease read this thread.
Sean T. Unwin
28,690 PointsWhat is the order of your .js
files in the HTML?
Martin Lindsey
11,273 PointsHere'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
28,690 PointsI 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.
Martin Lindsey
11,273 PointsTurns out it was a cache issue. I cleared the deck of all the history and it works fine now. Thanks!
Martin Lindsey
11,273 PointsMartin Lindsey
11,273 PointsThis is the question.js file.