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

Lee Cockcroft
Lee Cockcroft
5,147 Points

Listing all 3 questions in correct/incorrect

Hi, im assuming its to do with my "buildList" function,

If i get 1 question wrong, it lists all 3 questions and answers, and 2 lines of the same for correct.

Need some fresh eyes to take a look please?

Thanks

function print(message) {

var outputDiv=document.getElementById("output").innerHTML=message;

}

var correct=[]; var incorrect=[];

function buildList(arr) { var listHTML ="<ol>";

for (var i=0; i<arr.length;i++){ listHTML+="<li>"+arr[i]+"</li>";

}

listHTML+="</ol>"; return listHTML; }

var questions=[

["what is my favorite color","yellow"], ["what is my favorite food","pizza"], ["what is my favorite football team","leicester"]

];

var count=0; var guess; var html;

for(var i=0; i<questions.length; i++){

guess=prompt(questions[i][0]); guess=guess.toLowerCase();

if(guess===questions[i][1]){

count++; correct.push(questions);

}

else{ incorrect.push(questions); }

}

html='You have guessed ' + count + ' questions correctly out of 3 total.'; html+="<h2>you got the answers correct</h2>"; html+=buildList(correct); html+="<h2> you got these questions wrong </h2>"; html+=buildList(incorrect); print(html);

Can you provide Markdown so it's easier to read your code? Here's how:

```javascript

// Code goes here

```

If you do this, when you post the answer/question it should look like this:

// Code goes here

Can you please provide this kind of formatting? Thank you!

2 Answers

You are pushing the entire questions array to your correct and incorrect variables. This will result in it printing out what is my favorite color,yellow,what is my favorite food,pizza,what is my favorite football team,leicester like you are seeing.

You need to do

correct.push(questions[i]);

and

incorrect.push(questions[i]);

and you should see the results you are looking for.

Happy coding!