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

Emanuel Vasconcelos
Emanuel Vasconcelos
4,248 Points

Possible bug in JavaScript Basics, Unit 3 Challenge task

This is my answer:

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

But it keeps telling me that I didn't define totalWidth with a var keyword when I clearly did. I even tried this code on a workspace and it worked just fine.

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

3 Answers

Steven Parker
Steven Parker
231,271 Points

Your solution is perfectly good from a practical standpoint, but apparently the challenge wants you do solve it without creating an intermediate variable. Do it that way and you'll pass.

But you may also want to report this as a bug to the Support folks.

I agree with Steven, reporting this to Support seems like a good idea. I doubt the intention was to forbid you from creating an intermediate variable to solve this.

I think you're right - technically, the code you have should be passing, since it works and doesn't violate the instructions.

I think what's happening here is that you're declaring an extra variable - widthAsInt. While it isn't wrong to do so, the challenge doesn't tell you to declare another variable, so it's probably looking for "totalWidth" after the third "var", not "widthAsInt", hence the message.

There are two quick ways around this:

Converting within the width variable

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

Converting within the calculation

var width = '190px';
var numOfDivs = 10;

var totalWidth = parseInt(width) * numOfDivs;
Steven Parker
Steven Parker
231,271 Points

That first one alters the contents of width which I don't think is part of the intent of the exercise. If that one passes I'd say it is also a bug.

It does pass - I tried both before posting them. I see what you're saying, but it seems debatable - in a real solution, you wouldn't declare something as a string only to immediately parse it separately, so it would seem that any solution that successfully parses the string and uses it in a calculation would satisfy the intent. Changing the variable's value would only be an issue if we actually still needed the string value for something, and we don't. Changing width's initialization value would definitely violate the challenge's intent, because it would subvert the need to parse, but I don't think changing the value later necessarily goes against the intent (I'll leave that up to them, though).

Steven Parker
Steven Parker
231,271 Points

Just like in the real world, you don't know if this code is intended to be expanded later in some way. We just don't know if the variable is intended to be used again in a way that would require it to stay intact. It would certainly be extremely odd to initialize a variable using a string with both a number and a unit designation if it was never to be used other than as a numeric value.

So on general principle I would consider altering any of the initial values when not explicitly asked to in the instructions would be a legitimate cause for a fail. But allowing that to pass is not as serious a bug as failing a solution just for creating an intermediate variable.

I don't disagree - my point is really that the challenge is focused on making sure you know how to convert - it's not set up to look like an actual solution. In a real solution, you simply wouldn't declare a pointless variable in the first place - you'd probably just convert it inline if you had to pull it from a user input or some other string-based location, but the challenge is set up otherwise so that you can convert it separately, to prove that you picked up the concept. The best solution is to do it all in one line without changing the variable, but sometimes when learning it's easier to read/follow if it's separated out a bit. I generally don't assume how comfortable someone is with reading code when I answer a question, hence my offering of a one-line and a separated solution. Normally you'd do that with an intermediate variable, but, well.. that didn't work, and that's why we're here.

You're correct that you usually wouldn't do that in a real solution, but what I'm saying is that this challenge isn't set up like a real solution - it's just meant to illustrate whether you can use the parse method with correct syntax. To that end, I think both of these solutions AND the one Emanuel had originally should pass - they all successfully use parseInt().

Again, I really don't disagree with what you're saying - just trying to accurately illustrate my point. I personally feel that challenges should grant a pass for following the instructions and obtaining the desired result, within reason. That's just based on my own opinion of the purpose of the quick challenges, though - I definitely get what you're saying, and do feel that promoting best practices is important too. If anything, this conversation has made me think about how deeply the code-checking code should go into making sure the answer is correct AND that best practices are encouraged.

Emanuel Vasconcelos
Emanuel Vasconcelos
4,248 Points

Thanks to both of you for your detailed answers. I tried the "converting within the calcutation" solution that Katie proposed and it passed. It was really interesting to see both of your perspectives. I will let the Support guys know about this exerciese and let them decide if they consider this a bug or not xD