My var's don't work within quotes?

Tell us what’s happening:
Describe your issue in detail here.
Pls help

Your code so far


var myStr ="FirstLine
\SecondLine\\r\ThirdLine"; // Change this line

var FirstLine = "nah" ;
var SecondLine = "nah" ;
var ThirdLine = "nah" ;

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15

Challenge: Escape Sequences in Strings

Link to the challenge:

Good question! No, within quotes everything is a string.

For using variables, you need them to be evaluated, there are at least 2 ways.

  • Template literals: use backticks ` instead of quote, and sorround the variable like this ${FirstLine}.

var myStr = `${FirstLine} etc etc!`

  • Concatenating:
var myString = FirstLine + ' ' + 'more text ' + SecondLine + "...more..."

Pay attention that I mixed quotes so you can see as long as it makes sense they work, but you should not mix quotes. Use either single ' or double ".

Tip: do not capitalize variables. Use camelcase, but keep first letter lowercase. You’ll find out why later :slight_smile:

@anon22924398 it’s awesome you want to help, but your explanation is completely unrelated from the challenge mentioned here, maybe check which challenge campers are asking about?

What are you trying to do? It’s unclear what you are asking about
Please tell us what’s happening

Note that a string should all be in a single line, you can’t have a string divided over multiple lines

1 Like

To me it is pretty clear what OP tries to do.

And if you just reply it is unclear to you, why do you tell me what it is? I’m perplexed XD

We will see if it is a mistake or not with OPs answer. I believe it is trying to put the variables into the string, simply that.

Or maybe the OP is trying the pass the challenge, going at it the wrong way

So maybe ask your question, get a reply, and I will notice it and the OP too. Flagging a reply just by your sense isn’t right imho.

And then saying it is unrelated to the challenge (not the post because that is in the title), and then asking what they ask, it just wrong too.

Also, if you have the same patience with a reply than with the post, we’ll get along better.

I am trying to help, and if I notice it doesn’t work, I don’t reply, or I review the post in more detail.

Apart from all this, your comment is unrelated to the post itself and it would be simple to message me.

BTW, in the case you need an explanation of the reply, it is this:

the exercise asks to write a string escaping quotes. I thought the OP wanted to include variables also, which should probably work as a solution. I may be wrong, but it is not

but your explanation is completely unrelated from the challenge mentioned here

that.

There is a slight missunderstanding.
OP actually tries to solve the challenge - your advice builds upon the code OP wrote, but this code is just wrong in the context of the challenge and your admirable effort to help runs the risk of confusing OP more than helping. That’s why I assume @ilenia wrote here and not privatly. So OP knows that what your wrote is not wrong, but won’t help solve the challenge :wink:

1 Like

Please read the challenge again - you are supposed to use special characters in this challenge.
First, don’t declare variables like FirstLine and so on - while @anon22924398 showed you how you can use those in strings, you don’t need them for this challenge.

What you need are escape-characters as listed in the task.
For examle, the string "Thing\tding" will be turned into Thing ding → the \t is replaced by a tab-space. Same for linebrakes and others. If there is a \ then the interpreter (the thing that reads JavaScript commands) will take the following letter and turn both into new behavior (like adding linebreaks or tabspaces).
So Hello_\nWorld will be logged as

Hello_
World

Ignore the color.

Well, it is your interpretation and ‘your admirable effort’ is not a respectful reply.
Also, missunderstanding doesn’t exist my friend, unless you went to far as to make a joke. XD

You’re very ready to help users and not to be polite to other helpers. Sorry, I just don’t really follow your deep ideas there.

Does it sound good ? :-1:

Really? Damn I’m sorry, english isn’t my first language so in my head this sounded nice ^^°

when i look at the finished product it says:
firstline
secondline
thirdline
not;
nah
nah
nah

What “finished product”? The code you wrote throws a syntax error because you tried to split the string over several lines but JavaScript expects the string to end on the same line it started:
SyntaxError: unknown: Unterminated string constant. (1:11)
(1:11) meaning first line, 11th character.

yeah but i looked up what other people did and i put it in but it didnt work for me.

This is what i go, but i want it to say hello not FirstLine. How do i do that

const myStr = “FirstLine\n\tSecondLine\nThirdLIne”; // Change this line

var FirstLine = “hello”;

var SecondLine = “im learning a lot”;

var ThirdLine = “have a nice day”;

i accidentaly presd enter while typing it but i looked up what other people did and i put it in but it didnt work for me.

This is what i go, but i want it to say hello not FirstLine. How do i do that

const myStr = “FirstLine\n\tSecondLine\nThirdLIne”; // Change this line

var FirstLine = “hello”;

var SecondLine = “im learning a lot”;

var ThirdLine = “have a nice day”;

Ok so you seemingly solved the challenge, right? Looks good.

Now there are two reasons, why you cannot change the text. First is the fact you declare the variables AFTER you want to use them. Code is executed from top to bottom, so you cannot expect JavaScript to use a variable before declaring it.

Second is what @santimir already adressed. If you want to combine variables with strings, you have to tell JavaScript that you are using a variable. JS will assume everything inside of quotes is plain text → it will not try to find a variable that might be called like a part of the string.

Glad you understood it, and it’d be nice to hear @ilenia apologizing about my deep misunderstanding of the title.

Everyone please remember that there is a human on the other end of the keyboard.

The initial problem in the first post wasn’t clearly described, so it’s understandable that people tried to help OP solve the challenge. That’s what the Ask For Help button is intended for.

Let’s not derail the conversation with demands for justice please.

If you want to talk to a specific person about how you feel they talked to you, that’s a great time to use direct messages. This topic is for OP’s question.


@roemergb if you want to use variables that hold strings, you should go back to the Word Blanks challenge and review how it was done there.

1 Like

whats a word bank im very confused