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 trialCalvin Secrest
24,815 PointsDid you use the `parseInt()` method to extract the number from the `width` variable?
How do I used 'parselnt()' for this variable to define width?
var width = '190px';
var numOfDivs = 10;
var totalWidth = 'parselnt(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
Robin Malhotra
14,883 PointsFirst of all, 'parselnt(width * numofDivs)' is a string literally containing parselnt(width * numofDivs). In order to use it as an executable function, remove the quotes.
Here's an explanation of why Roy Penrod's answer works and yours doesn't
In your example,
parseInt(width*numOfDivs)
will try to multiply width and numOfDivs, which is a string and an integer respectively. Now, you can't multiply a string and an Integer (well, python can, but that's a different story altogether).
In Roy's example,
parseInt(width)*numOfDivs
will first convert width to an Integer using the parseInt() method. Now that parseInt(width) is an integer, it can be easily multiplied with another integer to give the correct result.
Roy Penrod
19,810 PointsYou were close to getting it. Here's the code the challenge is asking you to add below the other code:
var totalWidth = parseInt(width) * numOfDivs;
Note: I verified the code passed the challenge.
Roy Penrod
19,810 PointsHey, Robin ...
That was a good explanation, but he had another problem with his code that stopped it from even attempting to run ...
He enclosed the equation in single quotes, turning it into a string and storing it in the totalWidth variable.
Remove the single quotes first and then your answer takes over.
Robin Malhotra
14,883 PointsThanks Roy Penrod !! Updated the answer.