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

Really battling to grasp this particular concept of concatenation. converting and then combining.

Trying to figure out where I am going wrong. I have tried a few different methods, but clearly I am missing some fundamentals.

app.js
var id = "23188xtr";
var lastName = "Smith";

var userName = id.toUpperCase();
userName + "#" + lastName;
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>

2 Answers

Its totally cool, let break this down! take a look at the code below, and we will go over it.

//your code
var id = "23188xtr";
var lastName = "Smith";

//issue spot

var userName = id.toUpperCase(); //remove the semicolon everything needs to be joined. 
userName + "#" + lastName; //last name also needs to be uppercase as well use toUpperCase() here as well as id
// no need to concatenate userName 


//new code 

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

var userName = id.toUpperCase() + "#" + lastName.toUpperCase();

you are very close in this, the key here is to use the "+" to concatenate the two variables plus the string "#". remember if you use a semicolon you are ending the line, its like a "break" or "stop in Morse code.

second, you are putting the variables id and lastName inside the variable userName. UserName does not need to be called because you are putting the other variables inside it. and use the "+" operator and remember to use the toUpperCase() method and you should be good to go.

if you have any more questions pleases feel free to help. That's what we are here to do.

Awesome, Thanks! Knew it was going to be something obvious! I do have a question though, is there no way to make the whole 'var userName' all 'toUpperCase' instead of having to do it individually?

I don't think so, because in this scenario you are concatenating a string, as well as variables, But I'll look into it and find out.

Thanks again Jacob!

Not a problem! any time Jacques.

Rich Donnellan
Rich Donnellan
Treehouse Moderator 27,696 Points

It is possible — I had success converting the output using one toUpperCase() method:

var id = "23188xtr";
var lastName = "Smith";
var userName = (id + "#" + lastName).toUpperCase(); // wrap concatenation in parenths

console.log(userName); // output: 23188XTR#SMITH

Yeah! Thanks Rich!!! Gotta love those ( ) ...

Rich,

Awesome!! well Jacques, there you go!