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 JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Semhar Yohannes
Semhar Yohannes
1,770 Points

My solution to quiz challenge 1 doest work, but seems everything fine to me

I tried this but it doesn't work. (I type the answer upper case). Any help appreciate! :)

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


var correct = 0;
var answer;
var soluzione;

var domandeSoluzioni = [["what is my name?", "SEMHAR"],["How old am I?", 31],["Where do I live","STOCCOLMA"]];

for ( var i=0; i< domandeSoluzioni.length; i+=1)

{

answer =parseInt(prompt(domandeSoluzioni[i][0]));
soluzione=domandeSoluzioni[i][1];

if ( answer ===  soluzione)
{ correct  += 1; }

}


print("You got "+correct+" answer(s) correct");

Edited for formatting.

3 Answers

You are running parseInt on your answer from the prompt. This will remove the text from your answer, therefore your non-integer answers will always be wrong. To fix this easily you can remove the parseInt, then change your === to == (when comparing user-input to the answer) This will compare the variable values without comparing the variable types. There are other ways to fix this, but this is the quick/easy path right now.

As a side note you may also want to look into running .toUpperCase() on the user input so the users input is case insensitive.

Semhar Yohannes
Semhar Yohannes
1,770 Points

I see. So in this case , when I have answers that are both text and number, how do I do to have both type of value considered? Do I have to type the number as text ("31") and remove the parseInt? It doesnt seem right... What do you think? Thank you

answer updated

Semhar Yohannes
Semhar Yohannes
1,770 Points

Oh, that is right. Thank you. Yes, I ll fix the uppercase function.