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
Luka Gik
2,288 PointsAny tips on how to display results better?
var correctAnswer = 0;
var wrongAnswer = 0;
var userAnswer = [];
var questions = [
['Year that World of Warcraft came out?', 2004],
['How many expansions does World of Warcraft have?', 7],
['Name of the last World of Warcraft expansion?', 'battle for azeroth']
];
function print(message){
document.write(message);
}
function parseLowerAnswer(answer){
if(isNaN(answer)){
return answer.toLowerCase();
} else {
return parseInt(answer);
}
}
for(var i=0; i<questions.length; i++){
var answer = prompt(questions[i][0]);
userAnswer.push(answer);
if(parseLowerAnswer(answer) === questions[i][1]){
correctAnswer++;
} else {
wrongAnswer++;
}
}
print('<h2>You answered ' + correctAnswer + ' questions correctly!</h2>');
print('<p>Your answers: ' + '\n' + userAnswer + '</p>');
print('<p>Correct answers: ' + questions[0][1] + ',' + questions[1][1] + ',' + questions[2][1] + '</p>');
1 Answer
Liam Clarke
19,938 PointsWhat is it you would like to improve? The questions or the string concatenation?
One suggestion would be to use Object literals
Something along the lines of:
var questions = [
{
question: 'Year that World of Warcraft came out?',
correctAnswer: 2004
},
{
question: 'How many expansions does World of Warcraft have?',
correctAnswer: 7
},
{
question: 'Name of the last World of Warcraft expansion?',
correctAnswer: 'battle for azeroth'
}
];
// This will start as an empty array and you will push these values in from the user
var answers = [
2004,
5,
"Lich King"
];
var numberCorect = 0;
// Display questions
for( var key in questions ) {
console.log( questions[key].question );
}
// Send answers to object NOTE: you can also send the users answers directly to the object, ive just kept them seperate to demonstrate
for( var key in questions ) {
questions[key].answers = answers[key];
}
//Number of correct answers
for( var key in questions ) {
if(questions[key].answers == questions[key].correctAnswer) {
numberCorect++;
}
}
//Display number of correct answers
console.log("You answered " + numberCorect + " of " + questions.length + " question correctly!");
//Display results
for( var key in questions ) {
console.log( "Question: " + questions[key].question + "\nYou answered: " + questions[key].answers + "\nCorrect answer: " + questions[key].correctAnswer + "\n\n" );
}
I've tried to water it down to the bare minimum just to show you the use of object literals and how you can start doing DRY coding with your quiz, allowing you to hold the results in one place and displaying results through key-value pairs.
Object literals are a good place to start in my opinion as you can get introduced into design patterns lightly, here's an article I highly recommend, Learning JS Design patterns, the guy who wrote this is very good it's my go-to when I'm looking for a refresh.
Heres the output in the console. First we display the questions, Second, we show how many were correct and finally we show the results
Year that World of Warcraft came out?
How many expansions does World of Warcraft have?
Name of the last World of Warcraft expansion?
You answered 1 of 3 question correctly!
Question: Year that World of Warcraft came out?
You answered: 2004
Correct answer: 2004
Question: How many expansions does World of Warcraft have?
You answered: 5
Correct answer: 7
Question: Name of the last World of Warcraft expansion?
You answered: Lich King
Correct answer: battle for azeroth
This is just for demonstration purposes, keeping it simple, If you'd like some more info on refactoring this into a best practice format, let me know
Good Luck