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?
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`
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?
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…
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
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.