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.
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.
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+=”?
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.
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.
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!
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”
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");
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 = " "
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”);