I get the a "ReferenceError: document is not defined" while trying to
var body = document.getElementsByTagName("body")[0];
I have seen this before in others code and didn't cause any trouble. Why is it now? The companied HTML page is just a div inside the body.
Next
the code is the following:
(function(){ var body = document.getElementsByTagName("body")[0]; function Question(question, choices, correctAns) { this.question = question; this.choices = choices; this.correctAns = correctAns; } Question.prototype.checkAns = function(givenAns){ if (this.correctAns === givenAns) { console.log("OK"); } }; function Quiz() { this.questions = []; } Quiz.prototype.showAllQuestions = function(){ this.questions.forEach(function(questions){ console.log(questions.question); }); }; Quiz.prototype.showQuiz = function(){ this.questions.forEach(function(questions){ for (var i=0; i " + questions.choices[i] + "
"); } }); }; var q1 = new Question("What is red?", ["Color","Animal","Building"],1); var q2 = new Question("Most popular music?", ["Latin","Pop","Rock"],2); var quiz = new Quiz(); quiz.questions.push(q1); quiz.questions.push(q2); quiz.showAllQuestions(); })();
Try the whole code in this link HERE