Doubt - JavaScript: Word Blanks

Doubt - JavaScript: Word Blanks
0

#1

Hello all,

So i’ve passed this part and i understood how it works, except for one thing.

So the exercise asks us to build a complete sentence from a noun, an adjective, a verb, and an adverb and they supply us with those specific strings at the bottom. This is my solution which passed the test:

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
// Your code below this line
var result = "My " + myNoun + " is really really " + myAdjective + " but he just " + myVerb + " really " + myAdverb + “!”;

// Your code above this line
return result;
}

// Change the words here to test your function
wordBlanks(“dog”, “big”, “ran”, “quickly”);

My question is: If i put this on the Developer Tools console and try to console.log(result); to see the final sentence, it comes back with:
VM156:13 Uncaught ReferenceError: result is not defined
at :13:13

If i wanted to console.log(result); how would i have to do this?

I know that this is not what is asked on the exercise, but i need to understand why it doesn’t work.


#2

where are you putting the console.log(result)?

If you are putting it outside the wordBlanks() function then console.log() will not know what the variable result is because result was declared inside the function and only lives inside the function


#3

Thank you @camelcamper.
I tried it a lot of times, repeating some of them (all outside) without even noticing i was outside the function…

Thanks.


#4

All good.
Another way for concatenating JS strings and variables is by wrapping the whole string in backticks instead of quotes and wrapping variables in a ${}

var result = `My ${myNoun} is really really ${myAdjective} but he just ${myVerb} really ${myAdverb}!`;

This way you can type out your string without all the "" and + and is much easier to read. They teach it a little later in the course


#5

Great to know. I’ll save this info for future reference.
Thanks again :slight_smile: