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 Basics (Retired) Working With Numbers Numbers and Strings

Artiium Arbyummi
PLUS
Artiium Arbyummi
Courses Plus Student 560 Points

Not understanding how to use parseInt

I am having trouble wrting the parseInt. I thought I understood how to use it

app.js
var width = '190px';
var numOfDivs = 10;
var totalWidth = parseInt("width * numOfDivs");
index.html
<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
</head>
<body>
<script src="app.js"></script>
</body>
</html>

2 Answers

Howdy!

Since width is a string, not a number, you have to get the number from the string with parseInt(). Then you should multiply the result with numOfDivs.

This should work:

var totalWidth = parseInt(width) * numOfDivs;

Good luck!

Kevin Beall
Kevin Beall
13,828 Points

I'm sure in this case parseInt isn't needed as 190px is a length value and considered an Int already. ParseInt is used to turn a string into a number. The only example I can think of is converting an input value.

var input = prompt('Please enter a number.');
document.write(parseInt(input));