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!

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) Storing and Tracking Information with Variables Using String Methods

Kennen Pflughoeft
Kennen Pflughoeft
2,749 Points

Getting at message that Task 1 is not passing, but if I concatenate this sting, shouldn't Task 1 not pass here?

I'm not sure how both task 1 and task 2 should pass once I concatenate this string, this makes it hard to trust this error message.

var id = "23188xtr";
var lastName = "Smith";

var userName = id.toUpperCase();
var userName += "#";
var userName += lastName.toUpperCase();
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>JavaScript Basics</title>
<script src="app.js"></script>

1 Answer

Jason Anders
Jason Anders
Treehouse Moderator 145,855 Points

Hi Kennen, The reason you are getting the error, is because your are using 'var' on each instance of the concatenation. The challenge complier just sees this error and returns that the first task is no longer passing, because the value for user name is now "lastName.toUpperCase(). Every time you use var, you are basically wiping whatever was in there before and putting in a new value.

The correct way to concatenate this string (and being DRY) is

userName = userName + "#" + lastName.toUpperCase(); //do not use var in the front.

Hope that makes sense. Jason :)