5 days ago

const solve = (intArray) => { return intArray.find((number, index) => number == index); };

4 months ago

For those who are saying that filter isn't working: const solve = (intArray) => { const result = intArray.filter(el => intArray.indexOf(el) === el) return parseInt(result); }

5 months ago

const solve = (intArray) => { let matchingInteger = ""; for (let index of intArray) { if (index == intArray.indexOf(index)) { return (matchingInteger = index); break; } } };

6 months ago

am new here and am comfuse already

8 months ago

My code, python class Solution: def solve(self, int_array): for i in range(len(int_array)): if i == int_array[i]: return int_array[i]

8 months ago

const solve = (intArray) => { function equalToIndex(num, index) { return num === index; } return intArray.findIndex(equalToIndex); };

10 months ago

const solve = (intArray) => { return intArray.filter( (x, i) => x[i] === i) };

9 months ago

This solution doesn't seem to be correct. 'x' is already the element in index 'i', to make it work I think it should be 'return intArray.filter( (x, i) => x === i)'

That said, using '.filter' is not giving the correct answer because '.filter' returns an array, while the problem suggests the answer should be a single value, not an array...

9 months ago

But the above solution got passed (because I came up with this solution in the first place as well), so there might be some issues with the solution testing, I am afraid...

10 months ago

const solve = (intArray) => { for(let index in intArray) { if(intArray[index] == index) { return intArray[index]; } } };

a year ago

const solve = (intArray) => { for(let i =0;i<intArray.length;i++) { if (intArray[i] === i) { return i; } } };

a year ago

Hi anyone please let me know why case one fails while second one not? (JUST INTERCHANGED the comparision)

Case 1

const solve = (intArray) =>  intArray.find((el, i) => i===el);

Case 2.

const solve = (intArray) =>  intArray.find((el, i) => el===i);

a year ago

Bit more basic:

let result = '';
for (var i = 0; i < intArray.length; i++) {
if (i === intArray[i]) {
result = intArray[i];
break;
}
}
return result;

2 years ago

const solve = (intArray) => {
return intArray.find((item, index) => index === item)
};

a year ago

good one, here is my solution:

const solve = (intArray) => {
const maps = intArray.filter((el, i) => el === i);
return maps;
};

2 years ago

Is this site flawed?

This solution returns the correct answer on my IDE:

const solve = (intArray) => {
const result = intArray.filter((n, i) => {
if (n === i) {
return n;
}
});

return parseInt(result);
}

2 years ago

Array.prototype.filter() returns a new array of the items that satisfy the filter condition. You should only be looking for the first match, not all of them. Also, not sure how parseInt() deals with parsing an array...

2 years ago

Hey guys, this solution I wrote works inside a REPL.IT:

const solve = (intArray) => {
const result = intArray.forEach(function(num, index){
if (num === index) {
console.log(num);
}
});
return result;
};

but does not work here. Any suggestions on what I need to change to get it passing here?

2 years ago

Array.prototype.forEach() does not return anything (well, it returns undefined) and is meant for DOING something FOR each element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). You could use one of the other array methods like filter() or map() to get something back, but I found here the best solution with the least overhead that finishes as soon as it finds a match is a good old for loop:

const solve = (intArray) => {
for(let i = 0; i < intArray.length; i++) {
if (intArray[i] === i) return i;
}
};

2 years ago

const solve = (intArray) => intArray.reverse().reduce((acc,n,i, arr) => n === (arr.length - i - 1) ? n : acc, 0)

2 years ago

const solve = (intArray) => { return intArray.find((num, index) => num === index); };

2 years ago

const solve = (intArray) => {
return intArray.find(int => int === intArray.indexOf(int))
};

2 years ago

const solve = (intArray) => { return intArray.find(el => el === intArray.findIndex(item => item === el)); };

2 years ago

Two find calls could be quite expensive!