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
N alway
23,524 PointsMy solution
var Quiz = function(){
this.score = 0;
this.correct = [];
this.wrong = [];
// questions and answers
this.questions = [
["what is the chemical symbol for 'iron'",
"fe"],
["what color do you get if you mix red and blue",
"purple"],
["in what year was javascript released",
"1995"]
];
};
Quiz.prototype.HTMLText = function (id,text) {
// insert into html
var element = document.getElementById(id);
element.innerHTML = text;
};
Quiz.prototype.questionArray = function (arraylist) {
var results = [];
for (var i = 0; i < arraylist.length; i++) {
var userInput = prompt(this.questions[i][0]);
results.push(userInput);
}
// collect players results and return an array of answers
return results;
};
Quiz.prototype.printArray = function (id,id2, array, message) {
this.HTMLText(id2, message);
var results = "<ul>";
for (var i = 0; i < array.length; i++) {
results += "<li>"+ array[i] +"</li>";
}
// print players answers to html
this.HTMLText(id,results + "</ul>");
};
Quiz.prototype.run = function (id,id2, array, message) {
// collect array of users answers
var result = this.questionArray(this.questions);
// check results agains correct results
for (var i = 0; i < this.questions.length; i++) {
// if blank cause user didnt enter anything!!
if(result[i] === null){
result[i] = " ";
this.wrong.push(this.questions[i][0]);
} else{
if (result[i].toLowerCase() === this.questions[i][1].toLowerCase()) {
this.score++;
this.correct.push(this.questions[i][0]);
} else {
this.wrong.push(this.questions[i][0]);
}
}
}
// results
this.printArray("correct", "correct_message", this.correct, "correct");
this.printArray("wrong", "wrong_message", this.wrong, "wrong");
this.HTMLText("score", "Overall Score = " + this.score);
};
var quizQuestions = new Quiz();
quizQuestions.run();
1 Answer
Chyno Deluxe
16,936 PointsGreat job on your solution! One suggestion I could recommend to practice your skills with JavaScript would be to only display correct/wrong answers if there are any.
For example. If the user answers all of the questions correctly, then it will only display the correct_message and the correct answers and not display the empty wrong_message element.
Keep up the great work.
N alway
23,524 Pointsthanks :) I could add an "if" statement that checks the length of correct and wrong. if(this.correct.length >=1){ this.printArray(); }
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 Points//Fixed Code Presentation