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

Norie Agbuke
PLUS
Norie Agbuke
Courses Plus Student 2,308 Points

parseInt()

Let's assume we have 10 <div> tags on a page. Each is 190 pixels wide. Using the two variables in this script, create a new variable named totalWidth that multiples the width by the numOfDivs variable. You'll need to use a JavaScript function to retrieve the number value from the string in the width variable

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

Andreas Wennberg
PLUS
Andreas Wennberg
Courses Plus Student 8,036 Points

Dear Agbuke,

This works for me:

var width = '190px';
var numOfDivs = 10;
var width = parseInt(width); //You have to store the result from the parseInt function somewhere.
var totalWidth = width * numOfDivs; //You used colon instead of semicolon here. 

Let me know if something is unclear.

Cheers!

I don't get it this should work var width = '190px'; var numOfDivs = 10; var totalWidth = parseInt(width); var totalWidth = width * numOfDivs;

Andreas Wennberg
Andreas Wennberg
Courses Plus Student 8,036 Points

No because you are still refering to width in your formula and width is a string, not a number. In your code, you parseInt() and store the variable in totalWidth. You have to store it in width.

Andreas Wennberg
PLUS
Andreas Wennberg
Courses Plus Student 8,036 Points

A recommendation on this one is to use the console in Chrome. If you are on Windows you can see it by hitting F12 and on Mac it is CMD + OPT + J.

When I ran your program I got "Uncaught SyntaxError: Unexpected token : on app.js:4"

It means something is wrong with the ":" symbol on line 4 in the app.js file. It gives a pretty good hint about what is wrong. Hope that helps you in the future.