Search code examples
javascriptblank-line

How to write non-words characters in a javascript snippet for a "MadLibs" style word game?


I am on FreeCodeCamp and I need to build a "Mad Libs" style word game (let's call it "Word Blanks". I have to create a "Fill in the Blanks" style sentence.

In a "Mad Libs" game, you are provided sentences with some missing words, like nouns, verbs, adjectives and adverbs. You then fill in the missing pieces with words of your choice in a way that the completed sentence makes sense.

Consider this sentence - "It was really ____, and we ____ ourselves ____". This sentence has three missing pieces- an adjective, a verb and an adverb, and we can add words of our choice to complete it. We can then assign the completed sentence to a variable as follows:

var sentence = "It was really" + "hot" + ", and we" + "laughed" + "ourselves" + "silly.";

Given the instructions above, I've been provided with a noun, a verb, an adjective and an adverb. I need to form a complete sentence using words of my choice, along with the words I've been provided.

I will need to use the string concatenation operator + to build a new string, using the provided variables: myNoun, myAdjective, myVerb, and myAdverb. I will then assign the formed string to the result variable.

I will also need to account for spaces in my string, so that the final sentence has spaces between all the words. The result should be a complete sentence.

Now, I wrote the following code to complete the exercise above:

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {

  var myNoun = "___";
  var myAdjective = "___";
  var myVerb = "___";
  var myAdverb = "___";    

  var result = "my " + myAdjective + myNoun + "loves to " + myVerb + "very " 
  + myAdverb;

  return result;
}

wordBlanks("dog", "big", "ran", "quickly");
wordBlanks ();

But I get the following errors while running test:

// running tests wordBlanks("dog", "big", "ran", "quickly") should contain all of the passed in words separated by non-word characters (and any additional words in your madlib). wordBlanks("cat", "little", "hit", "slowly") should contain all of the passed in words separated by non-word characters (and any additional words in your madlib). // tests completed

What the "all of the passed in words separated by non-word characters (and any additional words in your madlib)" are supposed to be here?


EDIT

I modified the code accordingly to the following and now works. The test was failing because the words provided in the variables had to be replaced by different ones, and also because the var result in the function was missing additional words and spaces--->

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
  // Your code below this line
  var result = "I have a " + myAdjective + " bed and the " + myNoun + " always " + myVerb + " very " + myAdverb;

  // Your code above this line
  return result;
}

// Change the words here to test your function
wordBlanks("cat", "little", "hit", "slowly");

The missing "non-word" characters were therefore the spaces and the additional parts of the sentence (I added the following, along with the provided words: "I have a " , " bed and the " , " always " and " very " ).


Solution

  • It sounds like the test is checking the output string to verify that it contains each of the passed in words, and also to verify that they are separated by something, e.g. spaces, punctuation and other words.

    It's failing because you are overwriting each of the inputs with this: "___". So the input words are lost and do not make it into the output sentence.