Tell us what’s happening:
Pulling from many sources, I was eventually able to get some code to pass " Intermediate Algorithm Scripting: Wherefore art thou".
The main function takes an array of object and an object as arguments (collection and source, respectively). In a subsequent filtering function I use “obj”. How does the function know that “obj” is the first argument and not the second?
many thanks in advance for your help.
–Jed–
Your code so far
function whatIsInAName(collection, source) {
var arr = [];
// Only change code below this line
let keys = Object.keys(source);
return collection.filter(function(obj) {
for (let key of keys) {
if (!obj.hasOwnProperty(key) || obj[key] != source[key]) {
return false;
}
}
return true;
})
// Only change code above this line
return arr;
}
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.
here you are using the filter method on the collection
the filter method takes a function as argument, and it feeds to that function one at a time the elements in the array on which it is used, keeping the elements for which the function returns true and discarding those for which it returns false
At each iteration, obj is one of the elements inside collection
How does the function know that “obj” is the first argument and not the second?
They are refering to this line i think
function whatIsInAName(collection, source)
Then eileen explains that:
they use a filter method on the collection it being the 1st argument because it feeds 1 at the time.
Which she therefore answer the question as to why the object is related to the 1st argument