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

My quiz is not defined error message?

I'm following the practice project in my own text editor and getting an error on quiz_ui.js line 14. It says my capitalsQuiz is not defined.

quiz_ui.js

var QuizUI = {
    // Checks if the quiz has ended, if not displays the next question
    displayNext: function () {
        if ( capitalsQuiz.hasEnded() ) {
            this.displayScore();
        } else {
            this.displayQuestion();
            this.displayChoices();
            this.displayProgress();
        }
    },
    // Displays the question text
    displayQuestion: function () {
        this.populateIdWithHTML( "question", captialsQuiz.getCurrentQuestion().questions );
    },
    // Displays the answer choices
    displayChoices: function () {
        var choices = captialsQuiz.getCurrentQuestion().choices;
        // Loops through all the choices in the array
        for ( var i; i < choices.length; i++ ) {
            this.populateIdWithHTML("choice" + i, choices[1]);
            this.guessHandler("guess" + i, choices[1]);
        }
    },
    // Displays game over message with the users score
    displayScore: function () {
        var gameOverHTML = "<hl>Game Over</h1>";
        gameOverHTML += "<h2>Your score is " + captialsQuiz.score + "</h2>";
        this.populateIdWithHTML("captialsQuiz", gameOverHTML);
    },
    // A convenience method to grab the HTML placeholder and display text
    populateIdWithHTML: function (id, text) {
        var element = document.getElementById(id);
        element.innerHTML = text;
    },
    // Event handler to add score, move to the next question, and display it's the HTML
    guessHandler: function (id, guess) {
        var button = document.getElementById(id);
        button.onClick = function () {
            captialsQuiz.guess(guess);
            QuizUI.displayNext();
        }
    },
    // Displays the current question in the footer
    displayProgress: function () {
        var currentQuestionNumber = captialsQuiz.currentQuestionIndex + 1;
        this.populateIdWithHTML("progress", "Question" + currentQuestionNumber + " of " + captialsQuiz.guestions.length);
    }
};

app.js

// Create questions on country capitals
var question1 = new Question("What is the capital of England?", ["London", "Manchester"], "London");
var question2 = new Question("What is the capital of France?", ["Lyon", "Paris"], "Paris");

// Make a quiz out of an array of questions
var capitalsQuiz = new Quiz([question1, question2]);

// Display the quiz UI
QuizUI.displayNext();

Side note: are there some predefined functions/methods for workspaces? Following along with the videos I don't remember writing the method .hasEnded(). This could totally be me imagining things. I attempted my own in the quiz.js file:

quiz.js

/* 
A constructor to build a quiz object that contains an array of questions
You must add questions in the form of array
*/
function Quiz(questions) {
    this.score = 0;
    this.questions = questions;
    this.currentQuestionIndex = 0;
}

// A handy prototype to return the current question
Quiz.prototype.getCurrentQuestion = function () {
    return this.questions[this.currentQuestionIndex];
};

/*
A prototype to add a point to the user's score and
advance to the next question in the quiz array
*/
Quiz.prototype.guess = function (answer) {
    if ( this.getCurrentQuestion().isCorrectAnswer(answer) ) {
        this.score++;
    }
    this.currentQuestionIndex++;
};

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

1 Answer

It appears that you just misspelled capitalsQuiz as captialsQuiz in the quiz_ui.js file. Try correcting that to see if it works.

THANK YOU. I knew it was something small. Really appreciate it rvandaalen!