freeCodeCamp Challenge Guide: Word Blanks

freeCodeCamp Challenge Guide: Word Blanks
0.0 0

#1

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

:checkered_flag: 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.

Relevant Links

:speech_balloon: Hint: 1

+ can be used for concatenating strings.

try to solve the problem now

:speech_balloon: 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

:speech_balloon: 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

Spoiler Alert!

687474703a2f2f7777772e796f75726472756d2e636f6d2f796f75726472756d2f696d616765732f323030372f31302f31302f7265645f7761726e696e675f7369676e5f322e676966.gif

Solution ahead!

:beginner: Basic Code Solution:

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

    // 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.

:clipboard: NOTES FOR CONTRIBUTIONS:

  • :warning: 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.
  • Add an explanation of your solution.
  • Categorize the solution in one of the following categories — Basic, Intermediate and Advanced. :traffic_light:
  • Please add your username only if you have added any relevant main contents. (:warning: DO NOT remove any existing usernames)

See :point_right: 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
  
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");

#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:

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.


#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:

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”);


#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.