wordBlanksChallenge

wordBlanksChallenge
0

#1

Tell us what’s happening:

After squeezing my brain for a while to solve this challenge… I couldn’t. ha! So I turned to Get A Hint and the code written in there was:

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;
}

After this help, I managed to write smt that actually worked!:

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
// Your code below this line
var result = “The “+myAdjective+” ugly “+myNoun+” “+myVerb+” “+myAdverb+”.”;

// Your code above this line
return result;
}

Now, when I compared the codes, I realised that after the function there was a difference between them:

Get a Hint code:
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = “”;
result+=

What I wrote:
function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) {
var result = “The “+myAdjective+” ugly “+myNoun+” “+myVerb+” “+myAdverb+”.”;

As you can see, there’s no empty string (?) assigned to var result, neither += after result in what I did.

Now, my questions are: what is it? Why my code still worked without them? and how important they are?

It’s my very first week trying to learn JS and I am already crying! lol. But I’m not planning to give up!

Thanks for your help.

Your code so far


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

  // Your code above this line
  return result;
}

// Change the words here to test your function
wordBlanks("dog", "big", "ran", "quickly");


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/word-blanks


#2

Hello,

Happy to know you’re beginning on this beautiful journey of developing, but giving you a little bit of information of why they are starting the variable var result = “” , that’s usually to initialize a variable.

It’s a best practice to initialize a variable even though you are going to use in the next line. It makes the code more readable and friendly to changes in the future. You can read more about good practices in this article :

https://www.w3schools.com/js/js_best_practices.asp


#3

Clear and simple explanation. Thank you very much!