2

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;
}
Liam
30k28 gold badges142 silver badges204 bronze badges
asked May 21, 2015 at 13:46
9
  • 1
    Loop through the array until you find the value then return index... Commented May 21, 2015 at 13:48
  • 3
    your own attempt looks good except that you need to move "return index;" to the last line - outside of the loop. Commented May 21, 2015 at 13:49
  • Your return is in the wrong place, you want it after your for loop Commented May 21, 2015 at 13:49
  • His return is fine it can be in the loop he just needs to move it in the if statement. Commented May 21, 2015 at 13:50
  • 1
    If collection is actually an array, don't use for...in, use a regular loop for (var i =0; i < collection.length; i++). Then in the body of your loop check if collection[i] == target and if so return i. Commented May 21, 2015 at 13:51

6 Answers 6

7

This attempt is almost correct. You seem to already understand of what is required: loop over the elements of the array until you find a match, and then return the index of that match.

You've made a few mistakes, though. Let's walk through some improvements, step by step.

  • Your return statement is inside the loop, but outside of the if. You only want to return if you're found a match. Currently, you always return after the first iteration of the loop!

    function myIndexOf(collection, target) {
     var index;
     for(var val in collection){
     if(collection[val] === target){
     index = val;
     return index;
     }
     }
    }
    
  • There is no need for a separate index variable. You can return val as soon as you determine it's the correct answer.

    function myIndexOf(collection, target) {
     for(var val in collection){
     if(collection[val] === target){
     return val;
     }
     }
    }
    
  • You should loop using a numeric for loop, not a for-in loop. for-in loops are not guaranteed to have a set order, so you may not always get the lowest index that is a match. (Also, for-in could match on non-numeric property names, which might not be what you want.)

    function myIndexOf(collection, target) {
     for(var val=0; val<collection.length; val++){
     if(collection[val] === target){
     return val;
     }
     }
    }
    
  • To act just like indexOf, you could return -1 in the case that you don't find a match.

    function myIndexOf(collection, target) {
     for(var val=0; val<collection.length; val++){
     if(collection[val] === target){
     return val;
     }
     }
     return -1;
    }
    
answered May 21, 2015 at 14:00

1 Comment

What about target is a word.
2

Note: for..in should not be used to iterate over an Array where the index order is important.

for..in - JavaScript | MDN

var find_index = function(collection, item) {
 for (var i = 0; i < collection.length; ++i) {
 if (collection[i] === item) {
 return i;
 }
 }
};
find_index([5,4,3,2,1], 5)
answered May 21, 2015 at 13:53

1 Comment

Please provide some explanation since the OP is new to javascript. +1 anyways.
0

When looping through an array you should use a simple for loop from index 0 to Length-1 of your array. Don't use for...in because it can iterate properties of the array that aren't the actual contents of the cells and the order is not guaranteed. You want to find the first match to have the same behavior as .indexOf. So the correct implementation would look something like this:

function myIndexOf(array, target) {
 for (var i=0; i < array.length; i++) {
 if (array[i] === target) {
 return i;
 }
 }
 // item was not found
 return -1;
}
answered May 21, 2015 at 14:01

Comments

0

You can use in statement to iterate an array, assumings keys are values. Here val will be 0, 1, 2... so you can return it.

Then, you can use the return inside the if : it will stop the function and return the value just right you find what you are loonking for.

The === is the strict comparaison operator, checking var type, allowing you to test this is the exact value you are looking for.

You can add a return with an other value (here -1) if the value is not found at the end of the loop.

function myIndexOf(array, search) {
 for(var val in array){
 if(array[val] === search){
 return val;
 }
 }
 return -1;
}
var myArray = ['a', 'b', 'c'];
console.log(myIndexOf(myArray, 'c')); //2
console.log(myIndexOf(myArray, 'f')); //-1 <- Not found
answered May 21, 2015 at 14:02

Comments

0

This would work:

var index = 0;
var target = 'c';
var collection = ['a', 'b', 'c', 'd'];
function findIndex(){
 for(var val in collection) {
 if(collection[val] === target){
 return index;
 }
 index++;
 }
}
findIndex();
answered May 21, 2015 at 14:09

Comments

0

You can use every as well. Iterate through the array, return true if value doesn't match value being searched so every continues, otherwise set ind and return false.

var getIndex = function(arr, match){
 var ind = -1;
 arr.every(function(val, index) {
 if (val === match){
 ind = index;
 return false;
 }
 return true;
 });
 return ind;
}
getIndex([1, 2, 3], 2);
answered May 21, 2015 at 14:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.