Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Daniel Kol Adam
Daniel Kol Adam
3,285 Points

How can I get the "Israel" answer to work whether the user types it in caps, none caps, or just one letter of caps?

This is my code:

var questions = [ ["What year is it?", "2018"], ["Where is Tel Aviv?", "Israel"], ["How many days in a week?", "7"] ];

var correctAnswers = 0; var question; var answer; var response; var html;

for (var i = 0; i < questions.length; i +=1) { question = questions[i][0]; answer = questions[i][1]; response = prompt(question);

if (response === answer) { correctAnswers += 1; } }

html = "You got " + correctAnswers + " questions correct"; document.write(html);

1 Answer

Hey Daniel! You can use the .toLowerCase function. Say you have the following code:

var test = "QUIT"
test = test.toLowerCase()

test will now be "quit" instead of "QUIT", therefor just use the toLowerCase function on the prompt, so that everything they input is automaticly converted into lowercase, and then set the "Israel" to be lower case in the answer aswell. Hope this helps!

Daniel Kol Adam
Daniel Kol Adam
3,285 Points

And how would you put it in this code? when I apply it to prompt, it applies it to all 3 answers in the loop, which suddenly gives me 0 correct answers.

Kinda hard to tell you without supplying the code :P. However you cold simply do it like this:

var userInput = prompt("Input an answer").toLowerCase()

This code will convert whatever was inputted to all lowercase. And if you input non string it will do nothing. If you now set the "Israel" to just "israel" it will be the correct answer no matter how you inputted it eg:

var country = "israel"
var test = prompt("input something").toLowerCase()
if (country == test) {
alert("Yes the answer was correct..")
} else {
alert("No the answer was incorrect")
}

So regardless of wether they input IsRaEl or Israel or ISRAEL, it will be converted to israel, and then tested!

Hope that helps, if you need more help though, please show me the updated code!

Daniel Kol Adam
Daniel Kol Adam
3,285 Points

This is the code

var questions = [
  ["What year is it?", "2018"],
  ["Where is Tel Aviv?", "Israel"],
  ["How many days in a week?", "7"]
];


var correctAnswers = 0;
var question;
var answer;
var response;
var html;

for (var i = 0; i < questions.length; i +=1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question);

  if (response === answer) {
    correctAnswers += 1;
  }
}

html = "You got " + correctAnswers + " questions correct";
document.write(html); 

Yes now simply add the .toLowerCase() function to the prompt and change "Israel" to "israel". Code:

var questions = [
  ["What year is it?", "2018"],
  ["Where is Tel Aviv?", "israel"],
  ["How many days in a week?", "7"]
];


var correctAnswers = 0;
var question;
var answer;
var response;
var html;

for (var i = 0; i < questions.length; i +=1) {
  question = questions[i][0];
  answer = questions[i][1];
  response = prompt(question).toLowerCase();

  if (response === answer) {
    correctAnswers += 1;
  }
}

html = "You got " + correctAnswers + " questions correct";
document.write(html); 
Daniel Kol Adam
Daniel Kol Adam
3,285 Points

Thank you! I get it now !

Glad to help!