I do have another question which something that had been stumping me, sort like the increments aspect.
So in this exercise I need to modify the following code:
var myStr; // Change this line
to
Assign the following three lines of text into the single variable myStr
using escape sequences.
[Image removed as I’m new and can only post one, grr…]
I know that you can escape by using the . I just practiced that by quoting within a text string. I’m also given a reference guide:
Exercise with the guide for various escape notations. You can also check the objective of the exercise there.
So let’s think about this.
(1) After the FirstLine
& SecondLine
I need a carriage return, which is /r
.
(2) Before the SecondLine
& ThirdLine
, I need to have /n
because a new line is starting.
(3) I need a \\
before the SecondLine
preceded by /t
to get the same result (tab + backslash).
So I thought this would be the right answer:
var myStr \n FirstLine \r \tab \\ SecondLine \r \n ThirdLine ; // Change this line
…but it’s not. I get the following error saying I a syntax error with my \n
.
So I checked the following objectives for the exercise:
[See first link]
So I removed all the spaces and it still didn’t work:
var myStr\nFirstLine\r\tab\\SecondLine\r\nThirdLine ; // Change this line
I got the same error mistake actually.
So looking at the objectives, I found out that a new line doesn’t come at the beginning of a line but rather at the end. I had misunderstood.
So since it’s assigning a variable, I figured I’d need the = operator as well. Also, since they’re strings and not values, I need to use quotations which I had missed.
var myStr = 'FirstLine'\n'SecondLine' 'ThirdLine' ; // Change this line
Still getting the same error message, there’s something wrong with my syntax of \n
.
If I look at my objectives from top to bottom and try again:
var myStr = "FirstLine\n\t\\'SecondLine'\nThirdLine"; // Change this line
- I don’t have any spaces.
- All three strings are there.
- First line is followed by the newline character.
- The
\t
is there as well.
SecondLine
is preceded by the backslash character.
- There is a
\n
between the SecondLine
and ThirdLine
.
So I run…
Wow, great improvement. But still missing the last objective. Why is that? It’s clearly there:
So I figured maybe I messed up my quotations:
So I removed all the quotation marks and… I got it!
var myStr = "FirstLine\n\t\\SecondLine\nThirdLine"; // Change this line
The preview mislead me into making a mistake I think:
as the font is black instead of being red. I was trying to be exactly the same.
I wanted to share my reasoning so that people could point out where I have some fundamental misunderstandings. That’s why I took the time to share my stream of consciousness so I can rectify some misconceptions I already have before going further.