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 trialMartina Vonkomerova
7,790 PointsUI is not rendering
My code for QuizUI is:
var QuizUI= function(){
displayNext:function(){
if(quiz.hasEnded()){
this.displayScore();
}
else{
this.displayQuestion();
this.displayChoices();
this.displayProgress();
}
}
displayQuestion: function(){
this.propulateIdWithHtml("question", quiz.current().text);
}
displayChoices:function(){
var choices= quiz.current.choice;
for(var i=0;i<choices.length;i++){
this.propulateIdWithHtml("choice"+i, choices[i]);
this.guessHandler("guess",choices[i]);
}
}
displayScore:function(){
var gameover="<h1> Game Over <h1>";
gameover+= "<h2> Your score is:"+quiz.score+"</h2>";i
this.propulateIdWithHtml("quiz",gameover);
}
populateIdWithHtml: function(id,text){
var element = document.getElementById(id);
element.innerHTML= text;
}
displayProgress:function(){
var currentindex = quiz.current +1 ;
this.propulateIdWithHtml("progress", "Question"+currentindex+ "of" + quiz.questions.length);
}
guessHandler:function(id,guess){
var element = document.getElementById(id);
element.onclick= function(){
quiz.guess=guess;
QuizUI.displayNext();
}
}
}
4 Answers
Ace Motanya
31,756 Pointsvar 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);
}
}
Ace Motanya
31,756 Pointsits hard to read your code but umm try this and maybe you can compare and contrast and figure out where you went wrong
```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); } }```
Martina Vonkomerova
7,790 PointsThank your for your help, but it hasn't worked out yet.
christopherkaczenski
16,079 PointsNotice that the syntax for adding methods to object literals is slightly different than when using constructor functions.
QuizUI is an object literal, so there needs to be a comma after every method. Also, you do not need the "function()" keyword when defining an object literal with methods such as QuizUI.
Ace's code should work, but try clearing your cache.