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 trialAbirbhav Goswami
15,450 PointsHave a look at my code.
Here's my code attached. I'm leaving this here so that others can provide ways for me to improve. Any tips are appreciated! The code works just fine, and gives a nice result. But everything has scope of improvement, right?
var correctAnswers = 0;
var userAnswers = [];
var questions = [
["Name a programming language used to give a skeleton to webpages.",
"HTML"
],
["Name a language used to provide styling to a webpage.",
"CSS"
],
["Name a programming language released by Apple Inc. just two years ago.",
"Swift"
],
["Name one portal that teaches many types of programming languages through video lessons.",
"Treehouse"
],
["Which programming language is named after a famous mathematician?",
"Pascal"
],
["Which programming language shares its name with a snake?",
"Python"
],
["Which programming language is named after a gem",
"Ruby"
]
]
function print(message) {
document.write(message);
}
for ( var i in questions ) {
userAnswers.push( prompt( questions[i][0] ) );
if ( userAnswers[i].toLowerCase() === questions[i][1].toLowerCase() ) {
correctAnswers++;
alert( "That's correct!" )
} else {
alert( "Oops! The correct answer was: " + questions[i][1] );
}
}
function printQuiz( array ) {
var html = "<ol>";
for (var i in array) {
html += "<li>" + array[i][0] +"<ul><li> Your answer: " + userAnswers[i] + "</li>";
html += "<li>Correct answer:" + array[i][1] + "</li></ul></li>";
}
print( html );
}
printQuiz( questions );
print( "<h2>You got " + correctAnswers + " out of " + questions.length + " answers correct!" );
1 Answer
Lindsay Sauer
12,029 PointsLooks good Abby! One thing I notice is that you don't need to pass the questions array to printQuiz(), since this value will still be in scope. Then in printQuiz() change all references to array to questions.
Also just a minor thing, but your ending tag to the ordered list is missing: <<//ol>
Abirbhav Goswami
15,450 PointsAbirbhav Goswami
15,450 PointsThanks! Did that. Didn't even notice the ending ol tag to be missing. Added that as well.