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 1

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

doesn't log on console

// creating a new array var answers=new Array();

var correctAnswer=0;

//not yet used print function function print(message) { document.write(message); }

// questions

var questionList =[ ['states of US', '50'], ['capital of germany', 'berlin'], ['what are u drinking', 'cola'], ];

// ask the questions

for (var i=0; i<questionList.length; i++) { answers.push=prompt(questionList[i][0]); // check if the answers are correct if (answers[i]===questionList[i][1]) { correctAnswer +=1;

console.log(correctAnswer); }

}

1 Answer

Steven Parker
Steven Parker
231,269 Points

This line has improper syntax:

  answers.push=prompt(questionList[i][0]);

You probably meant to write this:

  answers.push(prompt(questionList[i][0]));

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

thanks a lot man. But I am still struggling to find the correct position to add my code for wrong answers and how to sort out the wrong answered array elements and run a loop for it. And the correctAnswer+=1 is also popping out for every correct answer.

var answers=new Array(); var correctAnswer=0;

function print(message) { document.write('<br/>'+ message); } var questionList =[ ['states of US', '50'], ['capital of germany', 'berlin'], ['what are u drinking', 'cola'], ];

// ask the questions

for (var i=0; i<questionList.length; i++) { answers.push(prompt(questionList[i][0])); // check if the answers are correct if (answers[i]===questionList[i][1]) { correctAnswer+=1;

//use to show var addPrint; addPrint=('You have answered '+correctAnswer+' questions correctly'); addPrint+=('<br/>'+questionList[i][0]+' '+ questionList[i][1]) ; print(addPrint);

var wrongAns; wrongAns ='\n you have ' +(questionList.length-correctAnswer)+ ' wrong answers'; print(wrongAns);

} else {

}

Steven Parker
Steven Parker
231,269 Points

After the point where you increment "correctAnswer", you could have an "else" that increments "wrongAnswer".

Then instead of logging "correctAnswer" every time you increment it, move the output to after the loop is over.