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 trialSemhar Yohannes
1,770 PointsMy 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");
3 Answers
Matt Green
3,557 PointsYou 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
1,770 PointsI 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
Matt Green
3,557 Pointsanswer updated
Semhar Yohannes
1,770 PointsOh, that is right. Thank you. Yes, I ll fix the uppercase function.
Jacob Mishkin
23,118 PointsJacob Mishkin
23,118 PointsEdited for formatting.