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 Functions Arrow Functions Testing for Number Arguments

Sara Masek
seal-mask
.a{fill-rule:evenodd;}techdegree
Sara Masek
Front End Web Development Techdegree Student 11,513 Points

Passing a string-type number results in a negative number for function outcome

Hi all, first just wanted to mention that I understood the lesson fundamentally, I'm just curious about why javascript accepts certain values in strings - in other words, I don't need help with this problem, I'm just curious if anyone has insight into WHY it results in the way it does below. Thank you!

const numGenerator = function(upper = 10,lower = 1){
  if ( isNaN(lower) || isNaN(upper) ) {
      throw Error("Both arguments must be numbers");
  }

  return Math.floor(Math.random() * (upper - lower + 1)) + lower;

};

console.log(` ${numGenerator(10,50)} is a random number `);
console.log( numGenerator(40,80) );
console.log( numGenerator(100,"300") );

The following console.log statements will then display, in order:

18 is a random number

67

-128300

I understand passing a string into a function that's expecting numbers will usually result in an error message, but that's if you type out the number itself (example: when I typed out "three hundred" then an error message displayed, but when I type "300" with the quotes it just results in a negative number every time.

I understand the code overall, I was just wondering why typing console.log(numGenerator(100,"three hundred") results in an error while typing console.log(numGenerator(100,"300") results in a negative number.

Reggie Williams
Reggie Williams
Treehouse Teacher

Hey Sara Masek check out the special behavior of isNan . It converts things like "300" to numbers

1 Answer

Hi Sara!

The difference is that "three hundred" is a true string.

"300" is what can be referred to as a "number string".

If you run some tests in the console, you will find that isNaN("three hundred") resolves to true.

isNaN("300"), oddly enough, resolves to false!?!

More info (scroll down to "Examples"):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN

This is similar to a case I encountered in Python.

When you request input from a user in Python, the input can only return strings.

So if a user types 123, it will return '123' and not 123.

But, if you try to cast '123' to an integer using int('123'), it will NOT error.

However, if you try to cast 'Sara' to an integer using int('Sara'), it WILL error.

I encountered that trying to validate user input for things like:

"Input your name: " (Should be a string, and NOT a number string)

"Input your age: " (Should only be a number)

I hope that helps.

Stay safe and happy coding!