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

Calvin Secrest
Calvin Secrest
24,815 Points

Did you use the `parseInt()` method to extract the number from the `width` variable?

How do I used 'parselnt()' for this variable to define width?

app.js
var width = '190px';
var numOfDivs = 10;
var totalWidth = 'parselnt(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

Robin Malhotra
Robin Malhotra
14,883 Points

First 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
Roy Penrod
19,810 Points

You 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
Roy Penrod
19,810 Points

Hey, 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
Robin Malhotra
14,883 Points

Thanks Roy Penrod !! Updated the answer.