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

My solution - review please

Hi, this is how I solved the challenge. Could I please receive some feedback on how to make it simpler/neater? Thanks!

var answer = ''
var correctGuess = 0
var wrongGuess = 0

var reportCorrect = [];
var reportWrong = [];

var questionAnswer = [
    ['What is 1 + 1?', 2],
    ['What is 2 + 2?', 4],
    ['What is 3 + 3?', 6]
];

for (var i = 0; i < questionAnswer.length; i += 1) {
    answer = parseInt(prompt(questionAnswer[i][0]))
    if (answer === questionAnswer[i][1]) {
        correctGuess += 1;
        reportCorrect.push(questionAnswer[i][0])
    } else {
        wrongGuess += 1;
        reportWrong.push(questionAnswer[i][0])
    }
}
    if (correctGuess > 0) {
        document.write("You answered " + correctGuess + " question(s) correctly!")
        document.write("<li> You answered these questions correctly:</li><ul>" + reportCorrect.join('<br />') + "</ul>")
    } else {
        document.write("<h2>You missed all the questions!</h2>")
}
    if (wrongGuess > 0) {
        document.write("<li> You answered these questions wrong:</li><ul>" + reportWrong.join('<br />') + "</ul>")
}

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

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

var answer = '' var correctGuess = 0 var wrongGuess = 0 var html = ''; var reportCorrect = []; var reportWrong = [];

var questionAnswer = [ ['What is 1 + 1?', 2], ['What is 2 + 2?', 4], ['What is 3 + 3?', 6] ];

for (var i = 0; i < questionAnswer.length; i += 1) { answer = parseInt(prompt(questionAnswer[i][0])) if (answer === questionAnswer[i][1]) { correctGuess += 1; reportCorrect.push(questionAnswer[i][0]); } else { wrongGuess += 1; reportWrong.push(questionAnswer[i][0]); } }

html += '<h2>You got ' + correctGuess + ' answers right</h2>'; html+= '<h2>You got these answer(s) right:</h2>'; html+= buildList(reportCorrect); html+= '<h2>You got these answer(s) wrong:</h2>'; html+= buildList(reportWrong); print(html);