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 Build an Object Challenge, Part 2 Solution

Nicholas Wallen
Nicholas Wallen
12,278 Points

How come half his numbers have quotes, and half don't?

Am I not understanding something, lol?

2 Answers

Steven Parker
Steven Parker
231,269 Points

Anything with quotes around it is a string, even if it contains digits. But since the only thing done with the values is to display them, there's no significant difference between storing numbers or strings in this case.

M Baker
seal-mask
.a{fill-rule:evenodd;}techdegree
M Baker
Full Stack JavaScript Techdegree Student 5,983 Points

Using the numbers as a string vs as an integer has different benefits depending on what your final goal is: If I were wanting to evaluate user input from a prompt() function call, such as in the quiz, and I wanted to skip the parseInt() step I could make the answer a string and use the strict equality comparison (===) with the same result:

var question = 'How many legs does a spider have?';

var answer = '8';

var userAnswer = prompt(question);

if (userAnswer === answer){

alert('You got it right!');

} else { alert('Sorry that is wrong');}

This works because JS evaluates strings that have the same characters in the same order to be equal. If we were evaluating two strings of characters however we would still need to ensure that the capitalization were the same between the two, using toUpperCase() or the like, however since numbers don't have capitals then we would only need to watch for the user entering the written number, such as 'eight'.

One last final note is that the downfall of doing it this way would make it much harder to test the input to ensure that a number was entered since you are asking for a string to compare to a string but both are a number value held in a string. The best solution here would be to evaluate the number with parseInt() without changing the value of the user input:

if(typeOf (parseInt(userAnswer) === 'number'){ //Put code here }

/* PLEASE LET ME KNOW IF I GOT ANYTHING WRONG HERE AS I DEFINITELY MAY HAVE */