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 trialKieran Corcoran
11,188 PointsBuild a Quiz Challenge, Part 1 Solution
Hi,
Can any tell me what might be wrong with this code. Does not appear to be converting the response toLowerCase method. If I remove that method it works fine but wonder why the toLowerCase method is causing an issue?
var score = 0;
var question;
var anwser;
var response;
var html;
var questions = [
['What is the capital of England', 'London'],
['What is the capital of Ireland', 'Dublin'],
['What is the capital of Wales', 'Cardiff']
];
for( var i = 0; i < questions.length; i++ ) {
question = questions[i][0];
anwser = questions[i][1];
response = prompt(question);
if( response.toLowerCase() === anwser ) {
score += 1;
}
}
function print(message) {
document.write(message);
}
html = 'You got ' + score + ' anwsers correct';
print(html);
2 Answers
Steven Parker
231,248 PointsAll of the answers are stored in mixed case (like "London"). So anything that is converted into lower case cannot match them.
But you're halfway there, now just change the stored answers to be lower case (like "london").
Siddharth Pande
9,046 Pointsfunction print(message) {
document.write(message);
}
var questArray = [
["Which gas helps in burning?","OXYGEN"],
["Who is the incumbent Prime Minister of India?","NARENDRA MODI"],
["Which state is the Capital of India?","DELHI"]
];
//All the main code is in this function:
function quiz (array) { //array is a parameter where argument will be passed.
var finalHtml = "<p>" //To produce the Html code
/*To make the
wrong answer Html Code*/
var wrongStr = "<h4>You got these questions wrong: </h4><ol>"
var correctAnswer = 0; // Counter for number of correct guesses
// To Make the correct Answer Html Code
var correctStr = "<h4> You got these questions correct: </h4><ol>";
//Engine(loop) where questions are asked and answers are compared by iterating through the loop and using a decision making if statement.
for(i=0; i < array.length; i += 1) {
var guess = prompt(array[i][0]);
if (guess.toUpperCase() === array[i][1]) {
correctAnswer += 1;
correctStr += "<li>" + array[i][0] + "</li>"
} else {
wrongStr += "<li>" + array[i][0] +"</li>"
}
}
//completing the Html code
finalHtml += "You got " + correctAnswer +
" question(s) right. </p> </br>" +
correctStr + "</ol> </br>" + wrongStr + "</ol>";
//Printing the final Html code on the document
print(finalHtml);
}
quiz(questArray); //Run the Program