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

Matthew Petrucci
Matthew Petrucci
4,756 Points

Unsure about Challenge 3 of 3: Combining Strings

I was able to pass this code challenge, but not in the way that I was expecting.

In the Google Chrome console, the following produces "Mary Jones":

var firstName, lastName, fullName;
firstName = 'Mary ';
lastName = 'Jones';
fullName = firstName + lastName;

console.log(fullName);

"Mary Jones"

However, this does not pass the code challenge.

Trying a different approach by surrounding the variables in quotes passed the code challenge, but doesn't produce the desired result in the Google Chrome console:

var firstName, lastName, fullName;
firstName = 'Mary ';
lastName = 'Jones';
fullName = 'firstName' + 'lastName';

console.log(fullName);

"firstNamelastName" 

Have I done something wrong?

4 Answers

James Barnett
James Barnett
39,199 Points

Dave McFarland - I think this is a problem with the code correctness check for step 3.


Matthew Petrucci -

What this code challenge is after is the idea of to adding a space when concatenating.

fullName = firstName + ' ' + lastName;

As a side note this is a much more flexible method to construct strings.


For instance what if later in your program you wanted to do you did ...

lastNameFirst = lastName + ", " + firstName;`

console.log(lastNameFirst);

"Jones, Mary "

Note the trailing space after Mary.

Jack Blankenship
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jack Blankenship
Full Stack JavaScript Techdegree Graduate 39,036 Points

Your first example is correct and does function as expected in Google Chrome console. If you have a different browser, you could retry the test using the first example and see if it will pass.

Matthew Petrucci
Matthew Petrucci
4,756 Points

Thanks for the quick response, Jack. I wasn't sure if I had made a mistake, or if the code challenge was expecting a pre-defined answer that doesn't include the first example as correct.

Hi Matthew,

The challenge isn't looking for an exact string match of "Mary Jones". That was only an example.

What it is expecting though is that you put a space between the firstName and lastName.

You could have passed with this: fullName = firstName + ' ' + lastName; although this would then produce 2 spaces because you put a space after 'Mary'

The challenge considers that trailing space to be part of the firstName variable. That space doesn't count as the separator between the 2 strings.

I'm at a loss as to why your 2nd code block passes the challenge. What you got in the console is what I would expect.

Dave McFarland ?

Dave,

In case it helps with the troubleshooting, this also passed for me:

var firstName, lastName, fullName;
firstName = 'Mary';
lastName = 'Jones';
fullName = 'foo' + 'foo';
Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Thanks everyone for your excellent troubleshooting. I've fixed the Code Challenge so that Matthew Petrucci's first code example now passes, and his second one doesn't.

I appreciate the feedback. Don't hesitate to post other errors like this to the forum, so I can fix them. I try to imagine as many possible correct and incorrect answers to test for in the code challenges, but I missed these!