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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2 Solution

Why does this code not work vs this code that does?

Hi there, I have two pieces of code that should do the same thing. The first code does not use a question and answer variable to store the prompt() first, and for some reason, returns undefined when trying to push to both incorrectArr and correctArr.. The second (commented out) code works as expected. Why does one code work but the other not?

//Not Workng Code
var correctArr = [];
var correctList = "<ol>";
var incorrectArr = [];
var incorrectList = "<ol>";
var userInput;
var myArr = [
    ['What is 2 + 2?', 4],
    ['How many states are in the United States?', 50],
    ['What year is your mustang?', 2003]    
];

for (var i = 0; i < myArr.length; i += 1) {
    userInput = prompt(myArr[i][0]);
    userInput = parseInt(userInput);

    console.log(userInput === myArr[i][1]);

    if(userInput === myArr[i][1]) { //if userinput equals the current answer
        correctArr.push(myArr[i][0]);
        console.log(correctArr[i]);
        correctList += "<li>" + correctArr[i] + "</li>";
    } else {
        incorrectArr.push(myArr[i][0]);
        console.log(incorrectArr[i]);
        incorrectList += "<li>" + incorrectArr[i] + "</li>";
    }
}

document.write("<h2>Questions answered correctly:</h2>" + correctList + "</ol><h2>Questions answered incorrectly:</h2>" + incorrectList + "</ol>");


/*
//Working Code

var question;
var answer;
var correctArr = [];
var correctList = "<ol>";
var incorrectArr = [];
var incorrectList = "<ol>";
var userInput;
var myArr = [
    ['What is 2 + 2?', 4],
    ['How many states are in the United States?', 50],
    ['What year is your mustang?', 2003]    
];

for(var i = 0; i < myArr.length; i += 1) {
    question = myArr[i][0];
    answer = myArr[i][1];

    userInput = prompt(question);
    userInput = parseInt(userInput);

    console.log(userInput === answer);

    if(userInput === answer) { //if userinput equals the current answer
        correctArr.push(question);
        console.log(correctArr[i]);
        correctList += "<li>" + question + "</li>";
    } else {
        incorrectArr.push(question);
        console.log(incorrectArr[i]);
        incorrectList += "<li>" + question + "</li>";
    }
}

document.write("<h2>Questions answered correctly:</h2>" + correctList + "</ol><h2>Questions answered incorrectly:</h2>" + incorrectList + "</ol>");
*/

1 Answer

It's kinda odd. I copy and pasted. Got an error for the very last line. There was an extra }. I commented that out and it worked fine. The odd thing this that extra brace doesn't show up just looking at your code here on the page. Maybe see if you left an extra brace at the bottom and comment it out like I did.