Basic JavaScript - Word Blanks

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

I was able to finally get my code to pass after looking at another person’s forum post. I’m posting in here though because I don’t understand why my code is written correctly and would like to understand this better so I can retain the information. The example that’s given has the values written in the spaces, not the variables, but in the challenge, I’m supposed to write in the variables. Why is this? Why is the example showing one way and then what I have to type out is another way? I’m assuming when I’m actually writing code for something I’d be writing in the values, not the variables, right? I’m maybe making this more difficult in my head, I do that A LOT unfortunately, and have really been struggling with understanding everything in the JavaScript course. Thanks in advance to anyone who can help me understand this.

Your code so far

const myNoun = "dog";
const myAdjective = "big";
const myVerb = "ran";
const myAdverb = "quickly";

// Only change code below this line
const wordBlanks = "My "+ myAdjective +" and dumb " + myNoun +" has "+ myVerb +" very "+ myAdverb +" into the street. "; // Change this line
// Only change code above this line

Your browser information:

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

Challenge: Basic JavaScript - Word Blanks

Link to the challenge:

This challenge is teaching you the principle of string concatenation, using variables. This is something which crops up a lot in programming.

In this example, it’s a means of creating customised text, which could generate different sentences, depending on the values of the variables.

If you console.log(wordBlanks) on the line after it is defined, you’ll see the whole sentence, which will change if you change the values of any of the variables above.

In practical terms, the values for those variables could come from form data or other user input, or data fetched from elsewhere (e.g. a user record in a database). This is just a simple implementation of how you can then use such data to create customised content.

Ok, so are you saying that by using the variables, instead of their values, it enables one to be able to change the values if needed at another point? Basically the variables are placeholders?

Yes. A variable is like a container for a piece of data. The label on the container is the name of the variable and the contents are quite literally ‘variable’ as new values can (usually) be reassigned to them. You declare a variable once (bringing it into existence), and then you can modify its value as required. In this case the values are strings, but they could also be numbers, objects, arrays or other forms of data.

1 Like

Awesome. Finally figured out something in JavaScript lol. Thank you for explaining it.

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