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

Richard Nash
Richard Nash
24,862 Points

Code does not run correctly...what am I missing?

Here is my code for the last challenge of Arrays section. But it won't run correctly. I got the questions to work before, but after adding all the final elements, the questions don't even appear anymore. What am I missing?

Here is my code:

//variables

var questions =
[
    ['What is 1 + 2?', 3],
    ['What is 2 + 3?', 5],
    ['What is 3 + 4?', 7]
];

var question;
var answer;
var response;
var html;
var rightAnswers = 0;
var rightQuestions = [];
var wrongQuestions = [];

//functions

function print(message)
{
    var outputDiv = document.GetElementByID('output');
    outputDiv.innerHTML = message;
}

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

//loop

for (var i = 0; i < questions.length; i ++ )
{
    question = questions[i][0];
    answer = questions[i][1];
    response = parseInt(prompt(question));
    if (response == answer)
    {
        rightQuestions.push(question);
        rightAnswers ++;
    }
    else
    {
        wrongQuestions.push(question);
    }
}

//messages

html = 'You got ' + right + ' questions(s) right.'
html += '<h2>You got these question(s) correct:</h2>';
html += buildList(rightQuestions);
html += '<h2>You got these question(s) wrong:</h2>';
html += buildList(wrongQuestions);

print(html);

I know it's a bunch of code to go through, but I don't know what is wrong.

Thank you for your time XD

Richard

PS. Maybe you can see what I did wrong Dave McFarland, if you don't mind :-)

Glad it's working, man!

Richard Nash
Richard Nash
24,862 Points

thanks Matt Brock, and I'm glad i asked, since i'm using the error console all the time now :-)

2 Answers

Problems I found:

for (var i = 0; i < array.length, i ++)
// Uncaught SyntaxError: Unexpected token )
html = 'You got ' + right + ' questions(s) right.'
// Uncaught ReferenceError: right is not defined
var outputDiv = document.GetElementByID('output');
// Uncaught TypeError: document.GetElementByID is not a function
Richard Nash
Richard Nash
24,862 Points

I see my error in the for loop, but I do not see the errors in the second two lines. And I do not know how to use Chromes Dev Tools to find errors in JS. Do you have an article or resource I could check out?

Thank you so far, btw :-)

  • "rightAnswers" is defined but "right" is not.
  • "document.getElementById" exists but "document.GetElementByID" doesn't.

Treehouse has a course on dev tools. Basically: right click -> inspect element -> console.

Richard Nash
Richard Nash
24,862 Points

ok, i fixed all those errors and i got the code to work when I replaced the guts of the print(message) function with:

document.write(message);

but it still does not work with the original guts of the print(message) function, even with 'Id' corrected...

...le sigh...

Richard, the "get" in "getElementById" should be lowercase:

var outputDiv = document.getElementById('output');
Richard Nash
Richard Nash
24,862 Points

hot damn, i'm stoopid! You were right Matt Brock! I had the G in "Get" capped but i simply did not see it!

Argh, it's all working properly now.

Thank you Floris Creyf and Eric Weintraub as well, and i am definitely getting on that debug console in chrome/firefox/safari/edge now. No more excuses! XD

Richard out...

Richard Nash
Richard Nash
24,862 Points

hot damn, i'm stoopid! You were right Matt Brock! I had the G in "Get" capped but i simply did not see it!

Argh, it's all working properly now.

Thank you Floris Creyf and Eric Weintraub as well, and i am definitely getting on that debug console in chrome/firefox/safari/edge now. No more excuses! XD

Richard out...

eric Weintraub
eric Weintraub
1,845 Points

Richard,

I am not sure what changes you made, but here is the working code:

https://jsfiddle.net/8ym7v4bn/