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 Foundations Numbers Parsing Numbers From Strings

JavaScript problem

var boxWidth = "640px"; var a = boxWidth.substr(0, 3); var numWidth = parseInt(a, 10);

  var boxHeight = "480px";
  var b = boxHeight.substr(0, 3);
  var numHeight = parseInt(b, 10);

Always get this error message: Bummer! Sorry! That's not correct. A correct answer will use the form: var numVariable = parseInt(stringVariable, radix);

However the above code can execute correctly. Anybody knows what's going-on?

1 Answer

Stone Preston
Stone Preston
42,016 Points

you dont need to use substrings. parseInt ignores any characters that are not numerals in the specified radix. see this documentation for more info.

 <script>
      var boxWidth = "640px";
      var boxHeight = "480px";
      var numWidth = parseInt(boxWidth, 10);
      var numHeight = parseInt(boxHeight, 10);



    </script>

I see, thanks.