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

JavaScript

String and parseInt

The instructor asked to create three questions by using an array and show the user the number correct. I was able to complete the challenge; however, mixing the question with numbers and strings. I was unable to do. I use parseInt to store my answers to the questions I asked. But when I had an answers with a string it did not count as correct.

Can you post your code?

3 Answers

If you can post your code that would be helpful.

var questions = [
  ['How many states are in the United States?', "50"],
  ['How many continents are there?', "7"],
  ['How many legs does an insect have?', "6"],
  ['What is the capital of California=?', "sacramento"]
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;

function print(message) {
  document.write(message);
}

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

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

html = "You got " + correctAnswers + " question(s) right."
print(html)

@Jessie Gibson...here you go. Not sure is this the best way to do this. I have mix questions with that contain numbers and strings as answers. I made all my answers to a strings.

Have you tried making all of your answers strings? Not just strings that contain numbers? The reason I ask this is because your if statement contains the .toLowerCase() method. I don't think you can lowercase a number. When I went through this challenge I found that it worked using either all numbers or all strings (containing words). Try those out and let me know

Thanks Mike for getting back to me. I will definitely look at this more.

No problem. Let me know how it goes