I am fairly new to JS and have a project to find the index of an array element, without using indexOf built in function. I have tried to search for solutions, but all I get are examples of the aforementioned built in function, which I already know how to use. Can anyone provide a simple example for me to go on? My mind goes towards something like this, but please note I am new to this and it is an academic exercise that I really want to understand:
var index;
var target = 10;
for(var val in collection){
if(collection[val] === target){
index = val;
}
return index;
}
ifstatement.collectionis actually an array, don't usefor...in, use a regular loopfor (var i =0; i < collection.length; i++). Then in the body of your loop check ifcollection[i] == targetand if so returni.