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

Ben Stanley
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Stanley
Full Stack JavaScript Techdegree Student 2,708 Points

Given 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.

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

Steven Parker
Steven Parker
231,269 Points

The error message is misleading.

:point_right: 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).

Ben Stanley
seal-mask
.a{fill-rule:evenodd;}techdegree
Ben Stanley
Full Stack JavaScript Techdegree Student 2,708 Points

Well 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
Steven Parker
231,269 Points

Still, what you were doing is legitimate. You may want to report it as a bug to Treehouse Support.

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Just change your totalWidth to use the parseInt function:

var totalWidth =parseInt(width) * parseInt(numOfDivs);

Steven Parker
Steven Parker
231,269 Points

numOfDivs is already a number, and doesn't need to be converted.