Word Blanks and understanding concatenation operators in JS

Word Blanks and understanding concatenation operators in JS
0

#1

here is my code:

  myNoun = "dog";
  myAdjective = "big";
  myVerb = "ran";
  myAdverb = "quickly";
  WordBlanks = "MyNoun + MyAdjective + myVerb + myAdverb";

I took a look at the solution, which is similar to mine but does a concatenation operator at the end with the blank string at the top, and also uses the += operator instead of +. I don’t completely understand these changes, or why my code fails.

Thanks for any help,

–Sam


#2

the above preview is actually a link to the exercise. it’s exercise 151.


#3

+= means your adding on to the variable but also changing it to equal the two strings
For example if I did

function exampleFunction(number) {
   number + 12;
   return number + 10;
}
exampleFunction(10)

It would return 20 but if I used a += operator

function exampleFunction(number) {
   number += 12;
   return number += 10;
}
exampleFunction(10)

It would add the number to the variable and change the variable to the new number twice so it would now equal 32

In other words a “=” changes the variable and “+” adds to it but doesn’t change it so a “+=” adds to it and changes it

P.S. I am 14 so correct me if I am wrong.


#4

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

You have three problems:

  1. JavaScript is case-sensitive, so make sure you type each variable the same throughout your code. For example, myNoun is not the same as MyNoun.

  2. One you resolve the typos, you need to understand that since MyNoune and myAdverb are variables, you do not want " before or after them.

  3. Resolving 1 and 2 above will still reveal one final problem. WordBlanks needs to be a sentence and sentences have spaces between the words. You just need to concatenate " " between each variable.


#5

You don’t need to set up any new variables (just use result). All of the variables should be passed in using the arguments of the function.

Your function should be something like this:

function wordBlanks(myNoun, myAdjective, myVerb, myAdverb) { // These are your variables
// They're set as whatever arguments you pass in to the function
// Don't assign new variables with the same name within the function,
// or you'll break it.
  var result = "";
  
  /* Here, you build up `result` into a sentence.
  You can either reassign `result` as the full sentence
  using `+` to concatenate, or build it up step by 
  step using `+=` */

  return result;
}

Examples of + vs +=

var a = '';
var b = '';

var c = ',';
var d = '!';

a = 'Hello' + c + ' World' + d; // reassigns `a`

b += 'Hello';
b += c;
b += ' World';
b += d; // building up `b` step-by-step

a; // "Hello, World!"
b; // "Hello, World!"

a === b; // true