I'm working on a JavaScript problem that involves hiding a parameter to demonstrate how this
works.
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.
Function definition:
function contains(cell) {}
How do I access the search space passed in as this
to the function? The accompanying testcase uses bind()
to pass in the search space e.g
contains.bind([[2,3], [3,4]])
Here is the attempt at completing the function:
function contains(cell){
let found = false;
[x, y] = cell;
// How do I access searchSpace passed in through `this`?
searchSpace.forEach((element) => {
[j, k] = element;
// match first occurrence in array
if (x === j && y === k){
found = true;
}
});
return found;
}
How do I invoke the function:
// contains(cell)
contains([1,2])