Template Literals Curiosity

Tell us what’s happening:
Since (result.failure) is already being specified ahead as the function’s argument, I have to ask: if we passed the whole array (result) as an arg of the func, could we specify arr.failure.map() at the const level?
I tried it and it doesn’t pass, however this was my first instinct(mostly because I failed to see the (result.failure) being passed in before) and I’m sure that I saw that being done before.

Tnks!

Your code so far

const resultDisplayArray = arr.failure.map(element =><li class="text-warning">${element}</li>);

const result = {
  success: ["max-length", "no-amd", "prefer-arrow-functions"],
  failure: ["no-var", "var-on-top", "linebreak"],
  skipped: ["id-blacklist", "no-dup-keys"]
};
function makeList(arr) {
  "use strict";
  const resultDisplayArray = arr.map(element =>`<li class="text-warning">${element}</li>`);
  return resultDisplayArray;
}
const resultDisplayArray = makeList(result.failure);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/create-strings-using-template-literals

Hey,

You are indeed correct, you could in another scenario that not this challenge send the full result object as a parameter and change inside the function to arr.failure.map(). While it would be assigning extra data that you’re not using it’ss indeed possible.

The reason it does not work for the challenge is that the tests that it is running in the background assume that you’re using the proper approach of sending to the function only what you’re going to need.

In other words, the test running in the background is calling the function makeList with makeList(result.failure) so it expects your code inside the function to be arr.map() and not arr.failure.map()

Extra info If you’re curious how to reach this conclusion change the inside of the function to arr.failure.map() and you will get on your console the message “Cannot read property ‘map’ of undefined”, that means that it is trying to access arr.failure and it is resulting in undefined so whatever is the parameter in the tests it’s for sure not an object with the property failure inside so you cannot use arr.failure since it’s sending an array most likely to the function (the parameter is named like an hint to that, arr instead of obj).

1 Like

Top answer. Thanks a lot! As for your extra info, I did just that and that’s precisely what I got. This will help a lot. I wish FCC was a bit more broad in the answers though. This way we could test some dif outputs…

1 Like