Word blanks lesson

I’ve been stuck on this lesson for about an hour, until I understood I had spacing issues.
This is my correct code

const wordBlanks = "We saw a "+ myNoun+ " It was a "+ myAdjective+ " Rotweiller "+ "It "+ myVerb+ " Very "+ myAdverb+ " Towards us"

Can anyone help me to understand how the spacing works exaclty? since I copied from a previous lesson without understanding the logic behind it.

“Watch out for spaces. Concatenation does not add spaces between concatenated strings, so you’ll need to add them yourself.”

I had to add a space between “We saw a (space)” or it would have been an error but why there? this isn’t not a space between strings it is very confusing.
It seems like a space between the last word and the quotation mark must be added is this true?

Hope you understood my question,! ty!

A string in JavaScript is anything between two single or double quotes, and it is literally on the characters between the quotes so you have to be explicit about every character in it.

You can kind of think of a string as a special array where every character is a specific element in said array:

'hello' //is similar to
['h', 'e', 'l', 'l', 'o']

'h e l l o' //is similar to
['h', ' ', 'e', ' ', 'l', ' ', 'l', ' ', 'o']

As you can see even the spaces are individual elements of this array.

Concatenation can be confusing and hard to read, but luckily we have template literals now which make it much easier to form a string dynamically. The template literal for what your string would be:

`We saw a ${myNoun} It was a ${myAdjective} Rotweiller It ${myVerb} very ${myAdverb} Towards us`

Documentation here:

Hey first of all thanks for your answer, it is very educational.
I want to add I am still a bit confused.

Let’s take a look at the cirriculum

const wordBlanks =  "We saw a" + myNoun + "It was a" + myAdjective + "Rotweiller" + "It " + myVerb + "Very" + myAdverb + "Towards us";

this should be correct according to your explanation, cirriculum and different stuff I see online, yet with this code I couldn’t pass the lesson, care to explain why ;p?

Here I made another correct answer on that lesson

const wordBlanks =   "We saw a " + myNoun + " It was a " + myAdjective + " Rotweiller " + "It " + myVerb + " Very " + myAdverb + " Towards us";

What is difficult for me to understand why do I need a space between “a” and the double quotes mark since they are part of the same string. except in the beginning of the sentence (because no strings before"W" & end of sentence) before the W in We, and the end of the sentence " Towards us", is this the way javascripts reads? or am I missing something…

Let’s put strings in place of the variables

const mySentence = "My cat" + "is" + "grey.";

Without spaces, you get

const mySentence = "My catisgrey.";

Same thing with variables.

so yeah, if there is a variable before or after a string, i have to add a space to my string, is this how it works? or is there an explanation to it?
Thanks for the help :stuck_out_tongue:

It has absolutely nothing to do with variables. Concatenation just puts all of the contents of the two strings into one string. It doesn’t add anything extra that you don’t say to add. If you want a space, you have to put it there yourself.

ahhh lol, now it makes everything really clear. Thanks.
It should say that on the cirriculum lol, says in a confusing manner.

Thanks a lot!

P.S. I’ve been researching eveywhere for this answer xD

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.