freeCodeCamp Challenge Guide: Concatenating Strings with the Plus Equals Operator

Concatenating Strings with the Plus Equals Operator


Hints

Hint 1

The ‘+=’ operator can concatenate (link) strings easily. Make sure your spelling is right, and you’ve left appropriate spaces.

var str = "Hello ";
str += "coding"; // Now the string reads "Hello coding"
str += "camper!"; // And now the string reads "Hello codingcamper!"
2 Likes

solution:

var myStr = “This is the first sentence.”;
myStr +=" This is the second sentence.";

10 Likes

I just did this one and it took me forever to figure out what I was doing wrong. Finally, I copied and pasted the above solution onto mine and compared. My difference/error was that I put a space in it at the end of the first sentence, like this:

var myStr = “This is the first sentence. “;
myStr +=” This is the second sentence.”;

When I removed the space, it was correct (like @annajuare’s) .

But, both the note on the problem and the example that was given express that space needs to be shown when concatenating strings.

It shows it as:

// Example
var ourStr = "I come first. ";
ourStr += “Iva come second.”;

// Only change code below this line

What am I doing wrong, @camperbot ?

6 Likes

//Incorect
var myStr = “This is the first sentence.”;
myStr += " This is the Second sentence.";

//Correct
var myStr = “This is the first sentence.”;
myStr +=" This is the second sentence.";

Only difference is a space after +=. Kept me thinking for 20 minutes.

11 Likes

Solution:

// Example
var ourStr = "I come first. ";
ourStr += “I come second.”;

// Only change code below this line

var myStr = "This is the first sentence. ";
myStr += “This is the second sentence.”;

5 Likes

The space is in the front of the second sentence.

3 Likes