freeCodeCamp Challenge Guide: Word Blanks

Word Blanks


Problem Explanation

We have provided a framework for testing your results with different words. The tests will run your function with several different inputs to make sure all of the provided words appear in the output, as well as your extra strings.

  • Change the code on the line with the comment //Change this line.
  • You will have basically created a sentence with the provided string variables.

Relevant Links


Hints

Hint 1

+ can be used for concatenating strings.

Hint 2

Just as you can chain strings by concatenating, you can assign them to an existing variable instead of a new one.

Hint 3

Remember to add your own non-letters in between each variable.


Solutions

Solution 1 (Click to Show/Hide)
var myNoun = "dog";
var myAdjective = "big";
var myVerb = "ran";
var myAdverb = "quickly";

var wordBlanks = "The " + myAdjective + " " + myNoun + " " + myVerb + " " + myAdverb + "."; // Only change this line;

Example Run

  • wordBlanks will be changed to string composed of the concatenated strings “dog”, “big”, “ran”, “quickly” through the variables myNoun, myAdjective, myVerb, myAdverb respectively; the order is changed and whitespace added.

Code Explanation

  • Use wordBlanks to concatenate the given variables.
  • Separate words with whitespace and appropriate words to form the full sentence.
61 Likes

I had to look at the solution on this page to figure out that I needed to use the “result” command. Could someone help me to understand the thought process that I should have used in order to figure out that I needed to use “result+=”?

Thanks!

Brian

26 Likes

Maybe the function portion of the code distracted you from the task at hand? The challenge wasn’t to figure out the intricacies of a JavaScript function but rather to modify the value for the variable “result”. This is no different than the exercises where we concatenated strings with plus/plus equals operators except we had to deal with more variables . I also got distracted myself till I looked this page and face-palmed myself to the next plane of existence. Oh well. Overthinking sucks.

21 Likes

Honestly I have no idea what the hell this question was asking me to do. And the answer left me more confused.

119 Likes

This is the second time this has happened to me. The first time, was with the Fahrenheit converter. Also, in what previous exercises, were we instructed to use “+myAdjective+”?

I understand that I could use read/search/ask to solve it, but quite honestly, exercises like this that leave out things like the above or that I needed to use “result” make me feel like I’m so stupid in not understanding the previous exercises that I can’t even solve the one in front of me. So much so, that I have to run and ask others for help.

If going somewhere else for help is what is expected, then please remove “We will now use our knowledge” from the exercise, as this indicates to me that I should be able to solve the exercise with what I’ve learned in previous exercises.

62 Likes

Hi there Brian, “result+=” basically means “result = result + whatever your other inputs”.
It actually works without the + or increment, because the result was previously defined as nothing
"var result = “” ".
Hope you understand, cheers!

10 Likes

I agree that this challenge is very poorly worded and the instructions leading up to this are unclear as to what you are supposed to do.

44 Likes

This problem has many problems. The instructions aren’t very clear, and you can pass the tests by doing pretty much anything.

9 Likes

That first one seems to pass, but the second one doesn’t, even though the output is closer to the requested output in the lesson.

It seems like the purpose of the lesson is to use string interpolation concat operators. Maybe we can rework the problem to be a bit clearer something like:

"Create a function that takes in as it’s input 4 string arguments and returns a new string with those 4 arguments.
"Each argument should be concatinated with a string literal on its left and right side using concatination operators.
“No two words should be joined together. The string’s white space should be as follows”
“Correct: My dog is very nice and likes to go to the store/”
“Incorrect: Mydoglikestogotothestore”

Just a few ideas to make this clearer.

10 Likes

Is it advisable to use this?

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
  var result = " ";
  // Your code below this line
  
result = "My " +result+myAdjective+ result+ myNoun +result+  myVerb +result+ myAdverb;
  // Your code above this line
  return result;
}

// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");
6 Likes

Hmmmm, I don’t think adding result to your string is the best way to go about it.
If I were to write this string there are a few ways I would do it.

let result = 'My' + ' ' + myNoun + ' ' + 'is very' + ' ' + myAdjective + ' ' +  'and he runs' + ' ' + myAdverb + '.'

let result = 'My ' + myNoun + ' is very ' + myAdjective + ' and he runs ' + myAdverb + '.'

I believe both of these should give the same output. I think the second one is easier because we are adding the white space to each word instead of separately.

EDIT: I see why you are using result now, it wasn’t clear to me before.

I think this is a source of confusion for people. Perhaps that variable should be
var emptyString = " "

6 Likes

A little confused about why we need to add the + sign both before and after the word being added:

result+= “My “+myAdjective+” “+myNoun+” “+myVerb+” very “+myAdverb+”.”;

So the leading + (in the first word) adds it to the word preceding, and the trailing plus adds it to the word following. Right?

So then why do all the following words have to have a leading + sign? in our example “+myNoun+” “+myVerb+” and “myAdverb+”.

I thought it was used to add space between the words, but that doesn’t seem to be the case. Without the trailing + it does not run.
Thanks.

3 Likes

Im not asking for an easy task… but please make it clear! so confusing for no reason

38 Likes

I solved it myself, without looking at the answer:

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "My " + myNoun + " Molly is really " + myAdjective + " and it " + myVerb + " really " + myAdverb;
// Your code below this line
// Your code above this line
return result;
}
// Change the words here to test your function
wordBlanks(“dog”, “big”, “ran”, “quickly”);

26 Likes

Hi campers! I know that this challenge is a bit confusing. Feel free to check out mine. Glad to help!

11 Likes

Edit: I checked out my code and figured that you can omit the code above of the result.

4 Likes

Okay. I did a double checked. And this is the result.

5 Likes

Use correct spacing and make sure to identify each of your variable wordBlanks! Here is the correct answer.

5 Likes

Thank God I wasn’t the only lost on this one. Some of these questions where you apply what you know are just ridiculous!!!

10 Likes

I just did it this way and it worked… but now seeing the way other ways, it makes more sense now. Haha, oh well.

2 Likes