Escape Sequences in Strings - Better Explanation of What to Do?

Tell us what’s happening:
Someone care to explain what this is even asking me to do? “Escape characters” sure but that doesn’t explain which ones it wants me to use and for what purpose. Or what they are even for? Why would I need to insert " \b " into a string in the first place, why not just not type the previous character in the first place (pretty sure that’s not what that does, but there’s no explanation so it seems as plausible as anything else). Browsing through previous forum question, there’s a bunch of examples of how to pass the test but no real explanations of what we’re supposed to be accomplishing. Also a lot of the solutions are throwing the " \r " in the mix where the checklist at the bottom of the console is saying add " \n "???

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.

Your code so far


var myStr= FirstLine
    /SecondLine
ThirdLine; // Change this line


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/escape-sequences-in-strings

1 Like

Hi, the point of this lesson is to teach you how to type certain special characters in JS.

I wouldn’t bother thinking about when you’re going to use each case, specially a “backspace” in a string.
It seems more productive to learn that the escape character \ is used for that purpose, So when you’re facing a situation which requires you to, say include a new line in your string, you will know that you need to use a backslash.

Finally, the challenge is to write a pre-formatted string:

FirstLine
    \SecondLine
ThirdLine

This string clearly has: two carriage returns, a tab and a backslash.
For each one of these special characters you will be needing the escape character.
For example, the text a "laser" has a double quote mark, if you want it to appear in your string, you could either type: 'a "laser"', enclosing the string in single quotes, or "a \"laser\"", enclosing it in double quotes and using the escape character.

1 Like

Not to sound completely ignorant or anything but carriage return as in the “enter” button? So that would be more or less the same as the new line which explains why some solutions in the forum used " \r" and others used " \n"? That was kind of what i was getting at. Granted thats probably 101 stuff but I don’t imagine I’m the only one coming at this from a design background with no real programming knowledge and and well this being a beginner course and all…

I did finally input the correct solution, having made a few attempts and I think the way the starter code was written had me thrown off. For starters, the way they were written almost made it seem like they were variables so at first i wasn’t using quotes, then also i seem to have missed where it was said that a variable was required to be written out on one line? I was copy pasting the original format and thinking the objective was to get the output on one line. All this was creating a syntax error so I couldn’t submit anything to get the work checked.

Some further thoughts on this, I was thinking that maybe it was just because markup hasn’t quite become second nature to me yet but looking at my original post, there’s only a subtle difference between the quote block and code block so given they were written in some half written camelcase, you might can see where the confusion would be. Perhaps the lesson would go over better if the quote block were written

First line.
      \Second line,
Third line.

or even better

Some text on the first line.
      \Some indented text on the second line,
Some more text on third line.

Also add

Don’t for get to indent using the tab escape sequence.

to the end of

with no spaces between escape sequences or words.

an then you can completely throw out this bs

Here is the text with the escape sequences written out.

FirstLine newline tab backslash SecondLine newline ThirdLine

Because

written out.

can easily be interpreted as indicating what would be printed out once the code was run. At least for someone in my position.

Lastly, if I’m not mistaken functions can be stored within variables, which until arrow functions came into play, were usually written out on multiple lines so

Assign the following three lines of text into the single variable myStr

could possibly be written

Assign the following three lines of text into the single line variable myStr

for those of us who haven’t quite grasped that code should be written on the least amount of lines necessary for it to still remain legible.

I guess I could just hop on github and submit a pull request. I suppose it is time I finally figured out how to do that sorta thing.

Thank you for your post. I don’t want to complain about FCC … because it’s FREE, and I’m grateful that it’s here and that people put time into it. Despite this, I was frustrated when I read this challenge because obviously anybody doing this challenge is a JS noob. After the basic lessons to this point, this sentence is very confusing: “[escaping characters] allow you to use characters you might not otherwise be able to type out” combined with a table that includes \n … newline. That makes me think, "So, if I type an n without a backslash in JS script it’s going to put the code on a new line? When? It can’t be every time. Huh? I wish that “n” would have been explained first, so that we would know why we would want to use a backslash. I know I’ll be able to pass this challenge from the information written in the forum, but, at this beginning point, I was glad to read the first line of your post: “someone care to explain what this is even asking me to do” because that’s how I felt, and I’m sure a ton of other people have felt the same.

1 Like