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 trialArtiium Arbyummi
Courses Plus Student 560 PointsNot understanding how to use parseInt
I am having trouble wrting the parseInt. I thought I understood how to use it
var width = '190px';
var numOfDivs = 10;
var totalWidth = parseInt("width * numOfDivs");
<!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
Calin Bogdan
14,921 PointsHowdy!
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
13,828 PointsI'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));
Artiium Arbyummi
Courses Plus Student 560 PointsArtiium Arbyummi
Courses Plus Student 560 PointsThank you very much!