I'm stuck here "Escape Sequences in Strings" anyone pls comment. Problem Solve!

Escape Sequences in Strings
Instructions
Assign the following three lines of text into the single variable myStr using escape sequences.

FirstLine
\SecondLine
ThirdLine
You will need to use escape sequences to insert special characters correctly. You will also need to follow the spacing as it looks above, with no spaces between escape sequences or words.

Here is the text with the escape sequences written out.

FirstLine newline backslash SecondLine backslash carriage-return ThirdLine

Here is what i write:
var myStr=“FirstLine\n”\""\SecondLine""\"\r"ThirdLine"";

Can’t find what’s wrong with this, anyone pls help?
Thanks

1 Like

You need to get rid of all those extra quotes. One at the beginning, one at the end.

var myStr=“FirstLine\n”\"\SecondLine""\\r"ThirdLine"";
strong text

The console come out different from what i write :myStr = “FirstLine\n”\“SecondLine”"\\r"ThirdLine""

lack of a \ before the word SecondLine. is that a problem?

1 Like

You don’t need any quotes inside the string. When you put ", that is the escape sequence for a quote. There should be only two quotes there, to enclose the string, but not in the string.

This is what you’re trying to build:

FirstLine
\SecondLine\
ThirdLine

Here’s what you need in the string. You need FirstLine Then (in the same string without any spaces or quotes, you need the escape sequence for a new line. That is just two characters, the backslash and the letter n. That gets you to the second line (even though we’re still in the same string). Then for that second line you need SecondLine - but note that they also want a backslash before and after that, so you’ll need escape sequences for that. Then, to get to the third line, you need a carriage return (another escape sequence). Then just he text ThirdLine

Note that this should be all one string, there should be no spaces in the strings, and there should be no quotes (except at the beginning of the string, like normal. There should be four escape sequences, a new line, a carriage return, and two for backslashes.

2 Likes

Thanks very much. i got the answer.

var myStr=“FirstLine\n\SecondLine\\rThirdLine”;

the backslash itself should become an escape consequence, i thought should be display as 2 backslash.

1 Like

The answer was already given above. I was just reiterating it. I’ve since removed it, realizing how old this thread is.

var myStr =“FirstLine\n\t\SecondLine\nThirdLine”; // Change this line

2 Likes