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 trialSahmad Nakumbe
3,423 PointsImagine you have 10 images on a web page. Each image is 190 pixels wide. Using the two variables in this script, create
const width = '190px';
const totalImages = 10;
const totalWidth = parseInt ( width * totalImages );
3 Answers
Steven Parker
231,275 PointsYou'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
13,589 PointsIs it possible to write like this: const totalWidth = +width * totalImages;
this didn't go through...tho Could someone tell me why?
Steven Parker
231,275 PointsType 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".
Karim Bouchtaib
1,651 PointsWhy mine is not working ?
const width = '190px';
const totalImages = 10;
const totalWidth = (+width * totalImages);
Natnael Kibreab
Web Development Techdegree Student 1,542 Pointsconst totalWidth = (+width * totalImages);
there is not need of + & you forgot the capitalization of width "W"