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 Project Overview

Dee K
Dee K
17,815 Points

My Solution. (Still need help though...)

//quiz.js
function Quiz() {
   this.quizzes = []; 
   this.currentIndex = 0;
}

Quiz.prototype.add = function(questions) {
  this.quizzes.push(questions);
};

Quiz.prototype.renderInElement = function(questions, answer1, answer2, number, score) {
    questions.innerHTML = "";
    answer1.innerHTML = "";
    answer2.innerHTML = "";

    if (number === this.quizzes.length) {
      var button1 = document.getElementById("guess0");
      button1.parentNode.removeChild(button1);

      var button2 = document.getElementById("guess1");
      button2.parentNode.removeChild(button2);

      var para1 = document.getElementById("choice0");
      para1.parentNode.removeChild(para1);

      var para2 = document.getElementById("choice1");
      para2.parentNode.removeChild(para2);

      var the_footer = document.getElementsByTagName("footer")[0];
      the_footer.parentNode.removeChild(the_footer);

      var h1 = document.getElementsByTagName("h1")[0];
      h1.innerHTML = 'Game Over';

      var h2 = document.getElementById("question");
      h2.innerHTML = 'Your score is: ';
      h2.innerHTML += score;
    }  
    questions.innerHTML += this.quizzes[number].toQuestion();
    answer1.innerHTML += this.quizzes[number].toAnswer1();
    answer2.innerHTML += this.quizzes[number].toAnswer2();
    };


Quiz.prototype.tracker = function(footer, number) {
  footer.innerHTML = "";
  footer.innerHTML += 'Question ';
  footer.innerHTML +=  number + 1;
  footer.innerHTML += ' of ';
  footer.innerHTML += this.quizzes.length;
};
//question.js
function Questions(question, answerOne, answerTwo) {
  this.question = question;
  this.answerOne = answerOne;
  this.answerTwo = answerTwo;
}

Questions.prototype.toQuestion = function() {
  //var questionPlacer = document.getElementById('question');
  //questionPlacer.innerHTML = "";
  var questionPlacer = this.question;
  return questionPlacer;
}

Questions.prototype.toAnswer1 = function() {
  //var answer1 = document.getElementById('choice0');
  //answer1.innerHTML = "";
  var answer1 = this.answerOne;
  return answer1;
}

Questions.prototype.toAnswer2 = function() {
  //var answer2 = document.getElementById('choice1');
 // answer2.innerHTML = "";
  var answer2 = this.answerTwo;
  return answer2;
}
//app.js
var quiz = new Quiz();

var historyQuestion = new Questions("Who was the first President of the United States?", "George Washington", "Thomas Jefferson");
var mathQuestion = new Questions("What is Batman's real name", "Clark Kent", "Bruce Wayne");

var answers = ["George Washington", "Bruce Wayne"];

quiz.add(historyQuestion);
quiz.add(mathQuestion);


var questionPlacer = document.getElementById('question');
var answer1 = document.getElementById('choice0');
var answer2 = document.getElementById('choice1');
var footer = document.getElementById('progress');

var number = 0;
var score = 0;

quiz.renderInElement(questionPlacer, answer1, answer2, number, score);
quiz.tracker(footer, number);

var button1 = document.getElementById('guess0');
button1.onclick = function() {
  number += 1;
  for (var i = 0; i < answers.length; i++) {
    if ( answer1.innerHTML === answers[i] ) {
      score += 1;
    }
  }
  quiz.renderInElement(questionPlacer, answer1, answer2, number, score);
  quiz.tracker(footer, number);
}

var button2 = document.getElementById('guess1');
button2.onclick = function() {
  number += 1;
  for (var i = 0; i < answers.length; i++) {
    if ( answer2.innerHTML === answers[i] ) {
      score += 1;
    } 
  }
  quiz.renderInElement(questionPlacer, answer1, answer2, number, score);
  quiz.tracker(footer, number);
}

My code appears to work fine. But when you get to the score page and open console, you get a TypeError.

How do I get rid of the TypeError?

Any suggestions would be appreciated!

Thanks!

Katie Jordan
Katie Jordan
7,842 Points

I don't have the answer for you but well done -- this is exceptional!