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

Greg Schudel
Greg Schudel
4,090 Points

getting errors, can't fix them

I cannot debug this code. Not sure why. Getting the following errors in console:

quiz_ui.js:38 Uncaught ReferenceError: text is not defined
    at Object.populateIdWithHTML (quiz_ui.js:38)
    at Object.displayQuestion (quiz_ui.js:17)
    at Object.displayNext (quiz_ui.js:10)
    at app.js:15

I checked the video several times and can't see what's wrong. here is my snapshot: https://w.trhou.se/imyjrwlvg3

Can you please share all of your code? Your app.js file in your link you posted is empty

8 Answers

Cory Harkins
Cory Harkins
16,500 Points
populateIdWithHTML: function(id, next) {
        var element = document.getElementById(id);
        element.innerHTML = text;

    }

You have = text, should be "next"

Cory Harkins
Cory Harkins
16,500 Points

In your questions.js file

function Questions(, 'How many total presidents have served in the U.S.?', 'What is the Answer to the Ultimate Question of Life, the Universe, and Everything?') {

 this.questions= [];

}

you have a comma inside your parameters that leads your string. Also, It's been a while since I've done this exercise, but I'm pretty sure your questions are in the wrong spot lol :-p

In your quiz_ui.js file, you are referencing object keys that aren't defined yet, that's why you get that text is not defined option.

The quiz.js file and app.js file is empty. Not sure how far you are into the exercise, however, those files will work in conjunction with quiz_ui.js

Greg Schudel
Greg Schudel
4,090 Points

okay?????????? For some reason the snapshot is not working correcting seeing how I have code in my app.js file when I go to launch the video, but in my snapshot there is nothing. Odd? Additionally, I am also unable to transfer the code to that snapshot to update it for some reason. Please bear with all my abundance of code seeing I have no other way to communiciate it

app.js

// array of questions
var questions = [
    new Questions("Who was the first President of the United States", ["George Washington", "Thomas Jefferson"], "George Washington"),
    new Questions("What is the answer to the ultimate Question of Life, the Universe, and Everything?", ["Food?", "42"], "42")
    // why did he write the options of the answers and then the answer? isn't that going against keeping your code DRY?
    // why did he type an array and then what looks like a array within an array for the two arrays above?

];


//this intiates the quiz
var quiz = new Quiz(questions);

//this displays the quiz
QuizUI.displayNext();

question.js

// constructor function for questions and input from user?
function Questions(text, choices, answer) {

    this.text = text; // why is this here?
    this.choices = choices;
    this.answer = answer;

}

// checks to see if it has the correct answer or the incorrect answer
Questions.prototype.isCorrectAnswer = function(choice) {

    return this.answer === choice;
};

quiz_ui.js

var QuizUI = {

    // I am compleletly lost at this syntax, NEVER seen it before in my life, 
    //is this an array with a function as the key of each value?
    displayNext: function(){
        if (quiz.hasEnded()) {
            this.displayScore();
        } else {
            // this updates all parts of the UI
            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, next) {
        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 getCurrentQuestion = quiz.currentQuestionIndex + 1;
        this.populateIdWithHTML("progress", "Question" + currentQuestionNumber + " of " + quiz.questions.length);
    }


};

quiz.js

//this keeps track of the totals for correct answers

function Quiz(question){
    this.score =0;
    this.questions = questions;
    this.currentQuestionsIndex = 0;

}


// evalues the answer
Quiz.prototype.guess = function (answer){
    if(this.getCurrentQuestion().isCorrectAnswer(answer)){
        this.score++; // if correct increments the score
    }
    this.currentQuestionsIndex++; /// moves forward by one in the array of questions

};

//this gets current question, but why create a prototype for getting it?
// if this is optional, then why did he create it again?

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

};


//ends the quiz when it reaches the limit of question
Quiz.prototype.hasEnded = function(){

    return this.currentQuestionsIndex >= this.questions.length;
};
Cory Harkins
Cory Harkins
16,500 Points

Also, the syntax is an Object.

Each key is the name of the "method". A function within an object is called a method. So... Math.floor(), Math is the object, floor is a method of that object.

There are a bunch of Objects/Methods that you will/have encountered.

MDN has a strong database covering these.

Cory Harkins
Cory Harkins
16,500 Points

Also... take a look at the error thrown at you.

quiz_ui.js:38 Uncaught ReferenceError: text is not defined
    at Object.populateIdWithHTML (quiz_ui.js:38)
    at Object.displayQuestion (quiz_ui.js:17)
    at Object.displayNext (quiz_ui.js:10)
    at app.js:15

when you see .js:## it is telling you what line # the syntax error is being caught at. It's not ALWAYS so, as JavaScript likes to be sneaky with it's bugs, but, for the most part, any syntax error or undefined value will be returned to you in the console.

What does the 'next' parameter do?

populateIdWithHTML: function(id, next) {
        var element = document.getElementById(id);
        element.innerHTML = text;

    },

Well the error you are getting is because 'text' in the above function does not exist. Did you mean to put 'text' as a function parameter? In what order are your javascript files being loaded in the html file? I see that you are referencing the quiz variable inside QuizUI. To comment on your comment about never having seen the syntax in QuizUI. What you are basically doing is to just create a regualr javascript object where each key is a function.

Greg Schudel
Greg Schudel
4,090 Points

Made more updates. NO errors in the console now....two things...

1.) I'm getting undefined in the area where the question should be. Not sure why.

2.) There is NaN showing up where the first question should be (yes I know what NaN is), just not sure why it doesn't work. Can anyone assist?

My up-to-date snapshot: https://w.trhou.se/8q6wudh7e5

Cory Harkins
Cory Harkins
16,500 Points

Check line 51 of quiz_ui.js attn to: currentQuestionIndex, is that the correct spelling, as in your other js files you reference currentQuestionsIndex....