Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,624 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 :)