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 trialBen Stanley
Full Stack JavaScript Techdegree Student 2,708 PointsGiven error of missing "var" with keyword "totalWidth" when both are present
I'm not understanding why I keep getting the error message. I have used parseInt(width) to call the width from the string but I'm given the error mentioned above when I go to check the work.
Directions are: 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 width = '190px';
var numOfDivs = 10;
var num = parseInt(width);
var totalWidth = num * 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
Steven Parker
231,275 PointsThe error message is misleading.
I think the challenge is expecting you to convert the width value on the same line as you do the calculation (don't use an intermediate variable).
Cindy Lea
Courses Plus Student 6,497 PointsJust change your totalWidth to use the parseInt function:
var totalWidth =parseInt(width) * parseInt(numOfDivs);
Steven Parker
231,275 PointsnumOfDivs is already a number, and doesn't need to be converted.
Ben Stanley
Full Stack JavaScript Techdegree Student 2,708 PointsBen Stanley
Full Stack JavaScript Techdegree Student 2,708 PointsWell that was it! Thanks for the response. I was looking over the teacher's notes and it was calling for 2 separate lines of code but I didn't realize I could just do it all on one line.
Thanks for the quick response!
Steven Parker
231,275 PointsSteven Parker
231,275 PointsStill, what you were doing is legitimate. You may want to report it as a bug to Treehouse Support.