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 Data Using Objects The Student Record Search Challenge Solution

tony abat
tony abat
8,321 Points

why does parseInt not work ?

"parseInt(prompt(question));" is not working in this challenge but "prompt(question)" does why is this?

3 Answers

Hugo Paz
Hugo Paz
15,622 Points

HI Tony,

Could you please link the challenge?

tony abat
tony abat
8,321 Points
var questions = [
    ['How many states ?','50'],
    ['How many Continents are there ?','7'],
    ['How many leggs on an Insect ?', '6']
];

var correctAnswers = 0;
var question;
var answer;
var response;
var correct = [];
var incorrect = [];

function print(message) {
    var outputDiv = document.getElementById('output');
    outputDiv.innerHTML = message;
}

function buildList(arr) {
    listHTML = '<ol>';
    for(var i = 0; i < arr.length; i += 1) {
        listHTML += '<li>' + arr[i] + '</li>'   
    }
    listHTML += '</ol>'
    return listHTML
}

for (var i = 0; i < questions.length; i += 1){
    question = questions[i][0];
    answer = questions[i][1];
    response = prompt(question);//parseInt(prompt(question));
    if(isNaN(response)) {
        alert('You must enter a number'); 
        questions.push(questions[i]);
        continue;
    }
 if (response === answer) {
        correctAnswers += 1;
        correct.push(question);
    } else {
        incorrect.push(question);   
    } 
}
tony abat
tony abat
8,321 Points
html = 'You got ' + correctAnswers + ' questions right';
html += '<h2> Here are the questions you got right </h2>';
html += buildList(correct);
html += '<h2> Here are the questions you got wrong </h2>';
html += buildList(incorrect);
print(html);
Hugo Paz
Hugo Paz
15,622 Points

You 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:

var message = 'What is your age?'
var test = parseInt(prompt(message), 10)

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
tony abat
8,321 Points

Thank 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
Hugo Paz
15,622 Points

I see what you mean, it is because you are using the triple === to compare the value of the response with the answer.

 if (response === answer) {
        correctAnswers += 1;
        correct.push(question);
    } else {
        incorrect.push(question);   
    } 

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
tony abat
8,321 Points

Thanks Hugo, I did put those numbers in quotes!! totally missed that! thanks man,

David Mesaros
David Mesaros
1,398 Points

Hi 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
tony abat
8,321 Points

Thanks David, I saw that after looking at Hugos post, thanks for the help!

It's also good practice to put all your variables at the beginning of the script :)