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

Numbers and Strings

Challenge 1 Task 1

I am using parseInt but I cannot figure out what goes in the ()

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

Hi Brenda,

With parseInt() you parse a string and it returns an integer.

In this scenario you will want to use parseInt() with the string variable width, to return an integer that you can then use to multiple with the variable numOfDivs.

An example, using your code:

var width = '190px';
var numOfDivs = 10;
var widthInt = parseInt(width); //Will convert the string '190px' to an integer '190'
var totalWidth = widthInt * numOfDivs; //Will multiple the integers together
Daniel Botta
Daniel Botta
17,956 Points

If you want to save a line of code, you could also just do this:

var width = '190px';
var numOfDivs = 10;
var totalWidth = parseInt(width) * numOfDivs;