简体   繁体   中英

Javascript: How does the carriage return work?

I am going through the Javascript course on FreeCodeCamp.com and am stuck at this challenge, 'Escape Sequences in Strings':

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 how I'm writing the code example:

var myStr = "Firstline\n\\Secondline\\\rThirdline";

The check requires there to be only one \\n . The \\r doesn't seem to function properly. I have tried different work arounds and searching hasn't provided any direction. I have tried using 'console' in the developer window of the browser and it appears \\n is working but the \\r is not.

  1. Is the carriage-return not working?
  2. Am I misunderstanding the lesson?

据我了解,回车符是在换行符之前,因此我认为必须使用/r/nThirdline才能使回车符/r/nThirdline

According to the book 'Javascript Bible' by Danny Goodman, chapter 15 'The String Objects', section 'Special Inline Characters':

"In most cases, browsers ignore carriage returns or render them like spaces."

https://books.google.com/books?id=WMr2NEG88icC&pg=PT262&lpg=PT262&dq=javascript+carriage+return&source=bl&ots=dWFLqN0rlO&sig=eZGo_74AHZq_4e2pxmtiqXz6l9s&hl=en&sa=X&ved=0ahUKEwivlMaEqu_QAhWBOSYKHSviBM44HhDoAQgjMAI#v=onepage&q=javascript%20carriage%20return&f=false

This might be a bug in how FreeCodeCamp checks this particular lesson. The code is probably rendered in the browser and so the check will always fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM