Tell us what’s happening:
I understand that the code can be used to call not only failure, but also others, by calling it at the end(like how it is done here to call failure).
But how do you give a value to the parameter of arr i.e z in this case?
Because, when I tested out this code, it showed undefined. So I’m assuming that there is some way to give a value to z.
Your code so far
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";
// change code below this line
const resultDisplayArray =arr.map(z =>
`<li class="text-warning">${z}</li>`);
// change code above this line
return resultDisplayArray;
}
/**
* makeList(result.failure) should return:
* [ `<li class="text-warning">no-var</li>`,
* `<li class="text-warning">var-on-top</li>`,
* `<li class="text-warning">linebreak</li>` ]
**/
const resultDisplayArray = makeList(result.failure);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.
Okay, I’m not sure I understand your question, but I’ll give it a go. The value of arr inside makeList() is derived from the argument passed into the function via the parens.
so when, at the end it calls makeList(result.failure); what is being passed into the function as arr is the value of the failure property of the result object which is an array.
makeList() doesn’t care where the array that gets passed in comes from. All it cares is that it gets an array you could call it like this: makeList(["Smy24", "Chad Kreutzer", "Quincy Larson"]); and it would work perfectly, returning:
You can omit the braces and the return only if you are returning a single expression. As soon as you put in those braces the return is required (if in fact you want to return something, as you do).
Yes and no. If your arrow function is just one line long and returning a value, you may omit the brackets and the return. if your arrow function is more than one line long, then you need to use the brackets and the return
When you call map on an array, your are iterating through each item in the array, performing some sort of operation on that item, then replacing that item with the results of your operation in a new array.
in this case, z stands in for that item each time.
it’s kinda like
const newArr = [];
for(let i = 0; i < arr.length; i++) {
// do something to arr[i]
newArr[i] = //the results of what you did
}
return newArr;
Inside map method your callback function is called with these parameters once for each element
current element
index of that current element,
the entire array
and a value to use as “this”
You are only using the first one - current element that you have named z.
The others are available but you haven’t created variables for those because you don’t need them.
Okay I’ll make my doubt more clear-
Arr itself is a parameter right? then why do we need to map another parameter inside it (z) in this case?
and how do we give inputs to z?
I’m sorry if my question makes no sense. I’m still figuring things out
because of that, arr inside your function takes on the value of the array that was passed in, and because of that, we can use any method on arr that we can use on an array.
so you mean to say that in the code above, z is nothing but “Hi, Smy24” and arr is nothing but str? and by giving a value to z, that value will be equal to arr?
const thisArray = ["cow", "horse", "elephant"] //original array
function myMap(callback){
const newArray = [];
// calls your function once for each element in array with those parameters
for(let i = 0; i < thisArray.length; i++){
newArray.push(callback(thisArray[i], i, thisArray));
}
return newArray;
}
myMap( el => `I'm so hungry I could eat a ${el}`)
// returned array
// [ 'I\'m so hungry I could eat a cow',
// 'I\'m so hungry I could eat a horse',
// 'I\'m so hungry I could eat a elephant' ]
No. if we were to map (hur hur) my example to your code above, arr would be like “Hi, Smy24” and z would be like the characters in “Hi, Smy24” one at a time.
lets assume we make “Hi, Smy24” into an array: const hi = ["H","i",","," ","S","m","y","2","4"]
applying map to that array like this:
hi.map(function(z){
return `${z}*`;
});
would iterate through that array and return a new one like this: `["H*","i*",",*"," *","S*","m*","y*","2*","4*"]