From d5798c38de82beb344b4c60e81205c7767b3e2f4 Mon Sep 17 00:00:00 2001 From: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Date: Tue, 9 Apr 2019 22:48:01 -0700 Subject: [PATCH] fix: rewrite of word blanks challenge (#35779) --- .../basic-javascript/word-blanks.english.md | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.english.md b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.english.md index 3fc9c119baf304..966390257a072f 100644 --- a/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.english.md +++ b/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/word-blanks.english.md @@ -10,13 +10,13 @@ videoUrl: 'https://scrimba.com/c/cP3vVsm' 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. 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.";
+
var sentence = "It was really " + "hot" + ", and we " + "laughed" + " ourselves " + "silly" + ".";
## Instructions
In this challenge, we provide you with a noun, a verb, an adjective and an adverb. You need to form a complete sentence using words of your choice, along with the words we provide. -You will need to use the string concatenation operator + to build a new string, using the provided variables: myNoun, myAdjective, myVerb, and myAdverb. You will then assign the formed string to the result variable. +You will need to use the string concatenation operator + to build a new string, using the provided variables: myNoun, myAdjective, myVerb, and myAdverb. You will then assign the formed string to the wordBlanks variable. You should not change the words assigned to the variables. You will also need to account for spaces in your string, so that the final sentence has spaces between all the words. The result should be a complete sentence.
@@ -25,12 +25,14 @@ You will also need to account for spaces in your string, so that the final sente ```yml tests: - - text: wordBlanks("","","","") should return a string. - testString: assert(typeof wordBlanks("","","","") === 'string', 'wordBlanks("","","","") should return a string.'); - - text: 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). - testString: assert(/\bdog\b/.test(test1) && /\bbig\b/.test(test1) && /\bran\b/.test(test1) && /\bquickly\b/.test(test1),'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).'); - - text: 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). - testString: assert(/\bcat\b/.test(test2) && /\blittle\b/.test(test2) && /\bhit\b/.test(test2) && /\bslowly\b/.test(test2),'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).'); + - text: wordBlanks should be a string. + testString: assert(typeof wordBlanks === 'string'); + - text: You should not change the values assigned to myNoun, myVerb, myAdjective or myAdverb. + testString: assert(myNoun === "dog" && myVerb === "ran" && myAdjective === "big" && myAdverb === "quickly"); + - text: You should not directly use the values "dog", "ran", "big", or "quickly" to create wordBlanks. + testString: const newCode = removeAssignments(code); assert(!/dog/.test(newCode) && !/ran/.test(newCode) && !/big/.test(newCode) && !/quickly/.test(newCode)); + - text: wordBlanks should contain all of the words assigned to the variables myNoun, myVerb, myAdjective and myAdverb separated by non-word characters (and any additional words in your madlib). + testString: assert(/\bdog\b/.test(wordBlanks) && /\bbig\b/.test(wordBlanks) && /\bran\b/.test(wordBlanks) && /\bquickly\b/.test(wordBlanks)); ``` @@ -42,27 +44,25 @@ tests:
```js -function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { - // Your code below this line - var result = ""; +var myNoun = "dog"; +var myAdjective = "big"; +var myVerb = "ran"; +var myAdverb = "quickly"; - // Your code above this line - return result; -} +var wordBlanks = ""; // Only change this line; -// Change the words here to test your function -wordBlanks("dog", "big", "ran", "quickly"); ```
- -### After Test
```js -var test1 = wordBlanks("dog", "big", "ran", "quickly"); -var test2 = wordBlanks("cat", "little", "hit", "slowly"); +const removeAssignments = str => str + .replace(/myNoun\s*=\s*["']dog["']/g, '') + .replace(/myAdjective\s*=\s*["']big["']/g, '') + .replace(/myVerb\s*=\s*["']ran["']/g, '') + .replace(/myAdverb\s*=\s*["']quickly["']/g, ''); ```
@@ -74,14 +74,13 @@ var test2 = wordBlanks("cat", "little", "hit", "slowly"); ```js -function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { - var result = ""; - - result = "Once there was a " + myNoun + " which was very " + myAdjective + ". "; - result += "It " + myVerb + " " + myAdverb + " around the yard."; +var myNoun = "dog"; +var myAdjective = "big"; +var myVerb = "ran"; +var myAdverb = "quickly"; - return result; -} +var wordBlanks = "Once there was a " + myNoun + " which was very " + myAdjective + ". "; +wordBlanks += "It " + myVerb + " " + myAdverb + " around the yard."; ```