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 Combining Strings

Beginner JS Concatenate code challenge help please.

var firstName; firstName=('Ian'); var lastName; lastName=('Hazelton');

/I can't get this line of code to pass./

var fullName=('firstName'+'lastName'); /error - don't put quotes around the variables/ var fullName=(firstName + lastName); /error - there should be an empty space character between first and last name/

Thanks for any replies.

Hi Ian,

This isn't related to your problem but there's a few things you can do to make your code simpler and shorter.

You can declare and initialize your variable in one statement.

Like this: var firstName = ('Ian');

Second, you don't need to have parentheses around your strings. Mainly you would use parentheses in this context when you wanted to change the normal order of operations when evaluating an expression.

Final code: var firstName = 'Ian';

Ken shows the reduced code in his answer.

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Ian;

No problem. Try:

var fullName = firstName + " " + lastName;

That concatenates an empty space between the two variables firstName & lastName.

Ken

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Ian;

As the challenge response is stating, you need an empty space between your variables. Right now your code of

var firstName="Ian";
var lastName="Hazleton";
var fullName = firstName + lastName;

sets fullName to IanHazleton. The challenge wants a space in there so that it would be Ian Hazleton. You can concatenate spaces with + " " +.

I'll just give you that clue for now, if you still are stuck, post back.

Ken

Thanks, Ken. I finally got it, but only because I used " instead of ' I didn't think it mattered which one you use, as long as you open and close them the same way. Using ' kept throwing syntax errors. Also, I got thrown off by the error message not to put quotes around the variables.

The quotes shouldn't matter. I passed with all single quotes. You just have to make sure you concatenated properly and that you have a space between your single quotes.

I thought it passed, but that was the previous questions. Still not getting this.

var fullName=('firstName ' + 'lastName');
var fullName=('firstName' + ' lastName');
var fullName=('firstName ' + ' lastName');

with and without brackets. I feel totally stupid, but i'm just not getting it

Ken Alger
Ken Alger
Treehouse Teacher

Ian;

You don't need to put quotes of any kind around the variable name(s). If you wanted to include a space inside the variable name to simply pass through the challenge engine, you would need to do something like...

firstName = "Ian "; or lastName = " Hazelton";

That would get you through the challenge with fullName = firstName + lastName; but it is hardly a best practice to include leading or trailing spaces in your string values.

Hope it helps.

Ken

Thanks, Ken. I'm almost positive this wasn't covered in the training video. Unless I missed it..all the times that I watched it. Anyway, I'm never likely to forget it now. Thanks for the answer and for trying to get me to solve it myself.