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 Numbers Working with Numbers Numbers and Strings

Imagine you have 10 images on a web page. Each image is 190 pixels wide. Using the two variables in this script, create

script.js
const width = '190px';
const totalImages = 10;
const totalWidth = parseInt ( width * totalImages );

3 Answers

Steven Parker
Steven Parker
231,186 Points

You're close, but only the "width" needs conversion, the "totalImages" value is already a number and the math needs to be done outside of the conversion:

const totalWidth = parseInt(width) * totalImages;
Kate C
Kate C
13,589 Points

Is it possible to write like this: const totalWidth = +width * totalImages;

this didn't go through...tho Could someone tell me why?

Steven Parker
Steven Parker
231,186 Points

Type coercion (as invoked by he unary "+" trick) works when the string value only contains digits.
But the "px" in this string makes it only convertible with "parseInt".

Why mine is not working ?

const width = '190px';
const totalImages = 10;

const totalWidth = (+width * totalImages);  

const totalWidth = (+width * totalImages);
there is not need of + & you forgot the capitalization of width "W"