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

Hi! could someone tell me where I'm going wrong

Question: Imagine you have 10 images on a web page. Each image is 190 pixels wide. Using the two variables in this script, create a new variable named totalWidth that multiplies width by totalImages. Since the value of width is a string, you'll need to use a built-in JavaScript function to retrieve the number value.

Answer:

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

Mod edit: added code markdown. Check out the "markdown cheatsheet" below the comment box for syntax examples to help with code readability on the forum.

1 Answer

Cameron Childres
Cameron Childres
11,820 Points

Hi Arjun,

Your only issue is when you pass width to parseInt(). Since width is in quotes you're passing the string "width" as opposed to the variable. Remove the quotes and you're good to go.

For a deeper dive you could look at the documentation for parseInt(). Under return value:

NaN when ... the first non-whitespace character cannot be converted to a number.

parseInt("width") sees the "w" first, can't convert it to a number, and returns NaN

Thank you so much!