I have a 2d array like this:
const sample = [
['', 'Name 1', ''],
['Name 2', '', 'Name 3'],
['', 'Name 4', 'Name 5']
]
I'm trying to write a function to return true if the index of input name is either 0 or 1 otherwise return false.
I have this:
function isIndexZeroOrOne(arr, name) {
for (const row of arr) {
const i = row.indexOf(name)
return i === 0 || i === 1 ? true : false
}
}
However, it only seems to work for the first sub array in sample (i.e., sample[0])
It's not working for Name 2: console.log(isIndexZeroOrOne(sample, 'Name 2'))
It return false even though it should be true (because the index of Name 2 is 0)
What am I doing wrong?