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 trialMike King
1,001 PointsCannot get my code to add lowercase answers as correct. HALP
So I'm obviously new to JavaScript and I've been looking at my code for a while attempting to come up with solutions for having my answers (madrid, dublin, london) all accepted in lowercase.
It currently works fine when entering the answers in uppercase.
I can imagine that there are various ways to make this code more efficient so any feedback would be appreciated however I'm really just looking to understand why neither the following two approaches seem to work
a) answer = answer..toUpperCase (added below "answer = prompt(quiz [i][0])")
b) creating a function to convert the prompt's input to uppercase and then calling the function before the if statement.
code
```var quiz = [ ['What is the capital of Spain?', 'MADRID'], ['What is the capital of Ireland?', 'DUBLIN'], ['What is the capital of England?', 'LONDON'] ]; var answer; var score = 3; var correct = []; var inCorrect = [];
for (var i = 0; i < quiz.length; i += 1) { answer = prompt(quiz [i][0]) if (answer != quiz[i][1]) { inCorrect.push(quiz[i][0]) --score; } else { correct.push(quiz[i][0]) } }
function displayHTML() { document.write(message); }
var message = ("You got " + score + " question(s) right." + "<p><h2> You got these questions correct: </p></h2>" + "<p>" + correct.join('<p> ') + "</p>" + "<p><h2> You got these questions incorrect: </p></h2>" + inCorrect.join('<p> '));
displayHTML();```
1 Answer
Steven Parker
231,268 PointsYou can call the conversion function within the "if":
if (answer.toupperCase() != quiz[i][1]) {
And when posting code to the forum, use Markdown formatting to preserve the appearance (as I have done here).