Escape Sequences in Strings - new line

Escape Sequences in Strings - new line
0

#1

hello.

I am at the javascript course in the Escape Sequences in Strings lesson.

is this the right way to make a new line?

var myStr = “FirstLine \n SecondLine ThirdLine”;


#2

If i remember correctly you don’t need the “n” after the “” sign.


#3

Well, per the instructions, there should be no spaces.

You will need four escape sequences, the first is the new line (which as you have is \n) and then an escape sequence for each of the backslashes they want, and a final one for the carriage return. Again, there must be no spaces in your string.


#4

You do use it the right way ,’\n’ for a new line but you shouldn’t use spaces in the string. Watch out for those kind of errors that can hurt your code. They are hard to be detected and can spend a lot of your time. If you need some help detecting errors, you can try using some programs like checkmarx or others. They work pretty good I guess. But if you’re new to programming it’s recommended to learn how to do it also by yourself.
Good luck!
Michael.


#5

thanks Michael,

i tried withoput spaces and stild doesn’t work:


#6

Can you please cut and paste the string you are trying to do?

Is that your string, “first\n second”?

first of all, your string should start out as "FirstLine\nSecond… then it must continue on.

The capitalization needs to be exactly the same, there must be no spaces and when you see “FirstLine”, that is meant to be exact. where the text is in red, that is meant to be escape sequences.

Think of it this way, the base string is “FirstLineSecondLineThirdLine”. Then you need to insert four escape sequences in there, one for new line, two for backslashes, and one for a carriage return.

If you get stuck, cut and paste your string here and we can help.


#7

thanks mate, i still don’t understand where is my mistake:


#8

I don’t want to give you the answer, but you’re making progress.

The new line is in the correct place and so is the second backslash. And there are no spaces.

Looking at the requirements, you need a backslash after the newline (before “SecondLine”). Also, the escape sequence for the carriage return needs to be before “ThirdLine”, not after.

So, if you add in that backslash escape after “FirstLine” and move the carriage-return escape, you should be set.

Remember that the red letters in the suggestion tell you exactly where those things are supposed to go. Any deviation from that will fail.

Let us know if you still have issues.


#9

Your printed output would currently be:

"FirstLine
SecondLine\ThirdLine
"

(With first line break being a newline and second being a carriage return)


#10

May I suggest you use the search feature within FCC prior to posting a question that has been addressed many times?

https://forum.freecodecamp.com/search?q=escape%20sequences


#11

thanks mate, it is ok now


#12

thanks for the help, i got it


#13

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

this is not working for me, what am i doing wrong. All boxes checked except for first one.
Please Help


#14

I c/p my code and it deleted slashes automatically?? WTF.

var myStr = “FirstLine\n\SecondLine\\rThirdLine”;
is what I have. Not working.


#15

wow, i literally typed my code in with all proper slashes and characters and when I hit reply, what shows up is not what I typed. WTF is going on here?
“FirstLine slash n slash slash SecondLine slash slash slash r ThirdLine”;