freeCodeCamp Challenge Guide: Concatenating Strings with the Plus Equals Operator

freeCodeCamp Challenge Guide: Concatenating Strings with the Plus Equals Operator
0.0 0

#1

We can use the += operator to concatenate a string onto the end of an existing string variable. This can be very helpful to break a long string over several lines.

Note

Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you’ll need to add them yourself.

Example

var ourStr = "I come first. ";
ourStr += "I come second.";

"Concatenating Strings"
#2

solution:

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


#3

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 ?


#4

//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.


#5

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.”;


#6

The space is in the front of the second sentence.