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 trialDenis Mudrov
Courses Plus Student 541 PointsIt's asking me to create a variable named totalWidth and I did but it's saying that my code is incorrect.
Imagine you have 10 <div> tags on a web page. Each div is 190 pixels wide. Using the two variables in this script, create a new variable named totalWidth that multiplies the width by the numOfDivs variable. Because the width variable is a string, you'll need to use a JavaScript function to retrieve the number value.
var totalWidth = width * numOfDivs
var width = '190px';
var numOfDivs = 10;
var width = parseInt(190);
var totalWidth = parseInt(190)
<!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>
4 Answers
Steven Parker
231,271 PointsHere's a few hints:
- when they say "Using the two variables in this script" they mean use them by name
- you will use these in the calculation but you won't reassign either of them
- you calculation will require both variables
- only one needs to be converted to a number
furkan
11,733 PointsSo here is the code they gave you in the question:
var width = '190px';
var numOfDivs = 10;
And the question asks you to create a new variable called totalWidth which has the value of the width and numOfDivs variables multiplied together. Now, note that the width variable has a value of '190px' in a string. If we just use the string value in our new totalWidth variable, it will return an error, because you cannot multiply a number by the string.
Instead, we need to use a special function called parseInt(). The parseInt() function accepts a variable with a string value that contains a number inside the parentheses. So, after creating your new variable totalWidth, assign a value of parseInt() and multiply that by the numOfDivs, and that should give you the correct answer.
I hope that has helped.
Denis Mudrov
Courses Plus Student 541 PointsDo I just do, var totalWidth = parseInt(190px) * numOfDivs ?
Denis Mudrov
Courses Plus Student 541 PointsI'm still having problems understanding this assignment. Can someone tell me the exact formula I have to type in. I'm pretty sure I've tried out any formula I could possible think of and I'm still stuck.
furkan
11,733 PointsYou are close. You do not need to specify 190px again, you have already stored that in the variable width. Just replace the parseInt() value with the variable width, and that should do it.
Steven Parker
231,271 PointsTry applying the hints I gave you and if you still haven't passed, post what you have after the changes.
Christian Leanza
3,101 PointsThis is the correct code & should help you solve the problem!
var width = '190px';
var numOfDivs = 10;
var totalWidth = parseInt(width) * numOfDivs;