ES6: Create Strings using Template Literals Andrej

Why does this code

  "use strict";

  // Only change code below this line
  const resultDisplayArray = arr.map((a) => {
  return `<li class="text-warning">${a}</li>`
});```

Require "return" to work. I thought when you used => for functions it returns your result without having to type return

Also can someone explain this code to me, I know that using arrow functions you put () at the beginning, so why are we putting arr.map in front and what does it mean/do?

Also what is the best way to post code? I thought it was with three back ticks in the beginning and the end.

Thanks,

Andrej

Hi @andrejjj222
Option 1:

Your right! "Required “return” to work, its because there are two ways of doing this with arrow functions, you either brake into a new line to return your value. this force you to add the {} to a new line and there for you need to add return with it,

Option 2:

But if you are still want to use arrow function and don’t want to return you don’t need to specify the {} because its telling the function to continue to do something else inside the code. So you will have something like this Note (in one line no curly braces and still will return your value). this will be a better way instead of adding return for shortness reasons.

"use strict";
var arr = [1,2,3];
  // Only change code below this line
  const resultDisplayArray = arr.map((a) => `<li class="text-warning">${a}</li>`);

The arr.map() the way it was written means creates a new array populated with the results of calling a provided function on every element in the calling array.
here is a link so you can learn more about it:

In addition to that you have and extra pair of braces arr.map((a) => ); with the letter a that’s the current Value of your array, you can leave it with braces or you can rewrite using this for better understanding and no need of extra braces. One option will be for two values you can have braces (a,b) for second option you can have braces with one value (a) or third option no need of braces. just your current value. a if its only one current value.

// Only change code below this line
  const resultDisplayArray = arr.map( a => `<li class="text-warning">${a}</li>`);
2 Likes