freeCodeCamp Challenge Guide: Word Blanks

freeCodeCamp Challenge Guide: Word Blanks
0

#1

Remember to use `Read-Search-Ask` if you get stuck. Try to pair program and write your own code

Problem Explanation:

We will now use our knowledge of strings to build a Mad Libs style word game weâre calling âWord Blanksâ. You will create an (optionally humorous) âFill in the Blanksâ style sentence.

You will need to use string operators to build a new string, result, using the provided variables: myNoun, myAdjective, myVerb, and myAdverb.

You will also need to use additional strings, which will not change, and must be in between all of the provided words. The output should be a complete sentence.

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 below `//Your Code here` and up to `//Change this line`.
• Take note that you are editing the inside of the `wordBlanks()` function.
• You will have basically created a sentence with the provided string variables.

Hint: 1

`+` can be used for concatenating strings.

try to solve the problem now

Hint: 2

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

try to solve the problem now

Hint: 3

`+=` will allow you to use an existing variable, a string type in this case. Remember to add your own non-letters in between each variable.

try to solve the problem now

Basic Code Solution:

``````function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = "";
// 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");
``````

Example Run

• Test `wordBlanks("dog", "big", "ran", "quickly");` runs.
• Variable result is declared with an empty string `""`.
• result will be changed with a new 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.
• result is returned.

Code Explanation:

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

NOTES FOR CONTRIBUTIONS:

• DO NOT add solutions that are similar to any existing solutions. If you think it is similar but better, then try to merge (or replace) the existing similar solution.
• Categorize the solution in one of the following categories â Basic, Intermediate and Advanced.

See `Wiki Challenge Solution Template` for reference.

Bai nay kha la ngan ne
#2

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

#3

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.

#4

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

#5

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.

#6

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!

#7

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.

#8

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

#9

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

Is it advisable to use this?

``````function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = " ";
// 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");
``````

#11

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 = " "

#12

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

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.

#13

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

#14

I solved it myself, without looking at the answer:

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â);

#15

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

#16

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

#17

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

#18

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

#19

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

#20

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