I helped a mentee with this problem. I was also not very clear on what was required.
The problem involves searching for an array, [1,2], in a search space which is a multidimensional array, e.g[[2,3], [3,4]]. However, the search space is passed in as part of this value to the function.
If we want to bind an object, [[2,3], [3,4]], as this to the contains function it would be contains.bind([[2,3], [3,4]]).
bind returns a new function, and that function then calls contains with the given value as this. If you check your test files, you will notice that bind is used to pass in the search space/state.
function contains(cell) {
console.dir(this); // will print out [[2,3], [3,4]]
console.dir(cell); // will print out [1,2]
}
const containsWithBind = contains.bind([[2,3], [3,4]])
containsWithBind([1,2])
So the contains function should look something like the following, note how this is used in the function:
function contains(cell){
let found = false;
[x, y] = cell;
// state available as `this`
this.forEach((element) => {
[j, k] = element;
// match first occurrence in array
if (x === j && y === k){
found = true;
}
});
return found;
}
Top comments (1)
Hola,
I helped a mentee with this problem. I was also not very clear on what was required.
The problem involves searching for an array,
[1,2], in a search space which is a multidimensional array, e.g[[2,3], [3,4]]. However, the search space is passed in as part ofthisvalue to the function.If we want to bind an object,
[[2,3], [3,4]], asthisto thecontainsfunction it would becontains.bind([[2,3], [3,4]]).bindreturns a new function, and that function then callscontainswith the given value asthis. If you check your test files, you will notice thatbindis used to pass in the search space/state.So the
containsfunction should look something like the following, note howthisis used in the function:References
this and bind