Error in "Escape Sequences in Strings"?

Error in "Escape Sequences in Strings"?
0.0 0

#1

The correct output should be:

FirstLine
\SecondLine
ThirdLine

and the corresponding code should be

var myStr = 'FirstLine\n\\SecondLine\\\rThirdLine'; // Change this line

However, I didn’t understand why carriage return was used in place of a newline. In my opinion, I thought that the correct code should be

var myStr = 'FirstLine\n\\SecondLine\\\nThirdLine'; //Change this line

To further check the validity of the two pieces of code, I added console.log(myStr); after the code and used Chrome’s Developer Tools (Ctrl+Shift+I) --> Console to see the output of myStr.

As a result, code that was
var myStr = ‘FirstLine\n\SecondLine\\nThirdLine’;
gave an output of
FirstLine
\SecondLine
ThirdLine

which is correct, while code that was

var myStr = ‘FirstLine\n\SecondLine\\rThirdLine’;
gave an output of

FirstLine
\SecondLine\ThirdLine

Thus, I see that there is an error in the solution to this challenge.
Am I correct?

Link to the challenge:


#2

It may not necessarily be an error.

In my Safari console I get the following:

> "a\nb"
> a
> b
> "a\rb"
> ab

While in the https://es6console.com I get this:

console.log("a\n\\b\\\rc")
> a
> \b\
> c

I think it’s just the difference of behavior between \n and \r and how it is dealt with in browsers. What the question asks for isn’t necessarily wrong, it’s just not consistently that way everywhere.

Maybe the big take away is … don’t use carriage return to make a new line! :slight_smile: (Which you learned!)


#3

I believe that there is a difference because carriage return works differently in Linux and Windows.

Also, I didn’t mean that there was an error. I was just asking whether the solution was incorrect, because the last time I checked, carriage return would return the pointer to the beginning of the line, and not to a new line, like \n does and what our output needs.

a
\b\ (\n)
c