The function find
return the index of the element in the array or -1 if it’s not found.
The only issue is that it’s suboptimal.
Improve the code and return the index of the element as soon as it’s found in the array.
This task is part of the Full-Stack JavaScript Course
If you have any issues with it, you can ask for community help below the post
Feel free to help others if you’ve already solved the task
helper.js
export const find = (arr, quote) => {
let index = -1;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === quote) {
index = i;
}
}
return index;
}
solution.js
import { find } from './helper.js';
const arr = [ 5, 8, -1, 10, 0, -5, 7 ];
console.log(find(arr, 10)); // 3
console.log(find(arr, 2)); // -1