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

Code Debugging: Attempting a Similar Quiz to Challenge Part 2

I'm attempting to build a variant of the quiz challenge, but seem to be running into some issues when I move my code over to a text editor (brackets.io).

https://w.trhou.se/3kn7j3zn8h

Any recommendations on what could I change to make my quiz.js code run?

can you elaborate on the issues? also can you post a code snippet?

//Quiz Bank 
var pokequiz = [
    ["How many different Pokemon can Eevee evolve into?", 8],
    ["How many Pokemon types exist?", 18],
    ["What year did Pokemon Red and Blue release in America", 1998],
    ["How much damage did the original base set Charizard's fire spin attack do?", 120]
];

//Setting Initial Vars
var Correct_Answers = 0;
var question;
var answer;
var response;
var correct = [];
var incorrect = [];

var html;

//Quiz logic
for (var i = 0; i < question.length; i += 1) { //Loop through all questions
    question = pokequiz[i][0]; 
    answer = pokequiz[i][1];
    response = parseInt(window.prompt(question));
  if (response === answer) {
    Correct_Answers + 1;
    correct.push(question);  
  }  
  else {
    incorrect.push(question);  
  }
}

//Print function
function print(message) {
  document.write(message);
}

//function for displaying questions
function buildList (arr) {
    var listHTML = '<ol>';
    for (var i = 0; i < arr.length; i += 1) {
        listHTML += '<li>' + arr[i] + '</li>;'
    }
    listHTML += '</ol>';
    return listHTML;
}

//HTML for outputting questions
html = "You got " + Correct_Answers + " question(s) right."
html += '<h2> You got these questions correct:</h2>';
html += buildList(correct);
html += '<h2> You got these questions wrong: </h2>';
html += buildList(incorrect);

//Output
print(html);

The errors I seem to be getting are: Line 23 - ERROR: 'window' is not defined Line 35 - ERROR: 'document' is not defined

1 Answer

//Setting Initial Vars
//Quiz Bank rather than nested arrays try having an array of objects.
const pokequiz = [
        {question: "How many different Pokemon can Eevee evolve into?", answer: 8},
        {question: "How many Pokemon types exist?", answer: 18},
        {question: "What year did Pokemon Red and Blue release in America", answer: 1998},
        {question: "How much damage did the original base set Charizard's fire spin attack do?", answer: 120}
    ], correctQuestions = [], incorrectQuestions = [];
//Setting Initial Vars
let correct = 0;
var html;
//Quiz logic 
//now you can loop your quiz accessing each object prompting with the questions. checking against the answer
pokequiz.forEach(function (quiz) {
    const response = parseInt(window.prompt(quiz.question));
    if(response === quiz.answer) {
        correct++;
        correctQuestions.push(quiz.question);  
    }   else {
        incorrectQuestions.push(quiz.question);  
    }
});
//Print function
function print(message) {
  document.write(message);
}
//function for displaying questions
function buildList (arr) {
    var listHTML = '<ol>';
    for (var i = 0; i < arr.length; i += 1) {
        listHTML += '<li>' + arr[i] + '</li>' //removed errant semicolon here that looked bad
    }
    listHTML += '</ol>';
    return listHTML;
}

//HTML for outputting questions
html = "You got " + correct + " question(s) right."
html += '<h2> You got these questions correct:</h2>';
html += buildList(correctQuestions);
html += '<h2> You got these questions wrong: </h2>';
html += buildList(incorrectQuestions);

//Output
print(html);

Thanks!