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

Question on the JavaScript Basics parseInt() Challenge

Here is my code: var width = '190px'; var numOfDivs = 10; var num = parseInt('190px'); var totalWidth = numOfDivs * num;

I don't understand why the correction response is asking 'Did you use the var keyword...?' when I submit my code. If I typed it in the 4th line statement, doesn't that mean I used the var keyword?

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

3 Answers

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Lynn,

Yeah, the "Bummer" hints are sometimes good, and sometimes kind of cryptic. In this case, it looks like the challenge is expecting only three variables (the original two - width and numOfDivs - plus the new one, totalWidth). You don't really need that extra num variable that you created.

Also, you can put a variable name inside the parseInt() function, like parseInt(width). As long as the variable named width contains a string that it can parse, it will work!

So in the line with var totalWidth = ... , you can replace num with your completed parseInt statement, per above. That way, you're telling JavaScript, set totalWidth to the numOfDivs x (whatever integer you can parse from the contents of the width variable).

I hope this helps!

Thanks! This is great. I struggle with understanding what exactly my instruction is telling the computer... or am I telling the program? Whatever it is I'm giving the instruction to, I'm still trying to grasp what I need to put inside the parenthesis

Steven Parker
Steven Parker
231,236 Points

While your solution is technically correct, you've done two things the challenge is probably not expecting:

  • the challenge likely expects you to use the variable containing the width string instead of repeating it.
  • the challenge probably expects the solution to be on one line, without creating another variable.

Make those changes and you'll pass. In the meantime here's a gold star for a workable (if not passing) solution.
:star2: :wink:

Thanks for nudging me in the right direction. I see where I went wrong.

Em Har
Em Har
9,775 Points

The answer is:

var width = '190px'; var numOfDivs = 10;

var totalWidth = parseInt(width) * numOfDivs

This one made me laugh! I don't think you're supposed to tell me the answer, but thank you for telling me the answer. I would have tried this challenge again but I've been without a computer for the past 2 days.