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 trialtony abat
8,321 Pointswhy does parseInt not work ?
"parseInt(prompt(question));" is not working in this challenge but "prompt(question)" does why is this?
3 Answers
Hugo Paz
15,622 PointsHI Tony,
Could you please link the challenge?
David Mesaros
1,398 PointsHi there,
I notice in your questions array that you added the 'string ' quotation to your numbers
var questions = [
['How many states ?','50'],
['How many Continents are there ?','7'],
['How many leggs on an Insect ?', '6']
];
- reviewing the video link, the instructor did not include the string quotation on the array for the numbers , the way you have it as above it wont work ie if you do console.log(typeOf answer) => String,
parseInt is to convert string to a number... where as prompt( questions) => String
if you removed the string on '50' , '7', '6' your code above should work with the parseInt.. I hope that helps
tony abat
8,321 PointsThanks David, I saw that after looking at Hugos post, thanks for the help!
Claudia Almeida
9,291 PointsIt's also good practice to put all your variables at the beginning of the script :)
tony abat
8,321 Pointstony abat
8,321 Pointsthanks, [https://teamtreehouse.com/library/javascript-arrays-and-loops/tracking-multiple-items-with-arrays/build-a-quiz-challenge-part-1-solution] around 3:34
tony abat
8,321 Pointstony abat
8,321 Pointstony abat
8,321 Pointstony abat
8,321 PointsHugo Paz
15,622 PointsHugo Paz
15,622 PointsYou can use parseInt if you like but it will only work if the first character is a number or a space. If not parseInt() returns NaN.
So if you have this code:
If you write 'my age is 25', parseInt will return NaN because the first character it finds is the 'm'.
Now if you write '25 years', parseInt will return 25.
tony abat
8,321 Pointstony abat
8,321 PointsThank you for taking the time to look at this I am aware of what you are saying but: The responses to the questions to this quiz must be a number, if not, an alert pops up and tells the user to enter a number. whenever i use the "parseInt(prompt(question));" the result is that all the number responses the user inputs results as incorrect. but when i use "prompt(question)" they all come back as correct. why is that? I am using Chrome on a mac, is this browser issue?
Hugo Paz
15,622 PointsHugo Paz
15,622 PointsI see what you mean, it is because you are using the triple === to compare the value of the response with the answer.
The answer is a string - ['How many states ?','50'] - 50 is a string. If you do parseInt to the response, you get an integer. While 50 == '50' returns true, 50 === '50' returns false because they are different types, string and integer.
tony abat
8,321 Pointstony abat
8,321 PointsThanks Hugo, I did put those numbers in quotes!! totally missed that! thanks man,