2

Trying to get this code to output -1 when value never occurs in an array.

function IndexOf (array,value) {
  var index = [];
  for (var i = 0; i < array.length; i++)
    if (array[i] === value) {
      index.push(i);
      return index.pop();
} else if (value === undefined) {
      return -1;
}
}

EDIT: not allowed to use .indexOf for this particular case.

EDIT 2: Sorry I wasn't more clear. I need to return the last matched element as opposed to first.

Binny.H
  • 83
  • 6
  • why don't you use `.indexOf` which already does that for you ? – Muhammad Usman Apr 22 '18 at 03:28
  • Not allowed to for this specific case – Binny.H Apr 22 '18 at 03:29
  • 3
    Because this is a code challenge lol solve it yourself. I believe in you – JJJ Apr 22 '18 at 03:31
  • I will give you a hint: you don't need the else if block – JJJ Apr 22 '18 at 03:35
  • Possible duplicate of [How do I check in JavaScript if a value exists at a certain array index?](https://stackoverflow.com/questions/2672380/how-do-i-check-in-javascript-if-a-value-exists-at-a-certain-array-index) – Mehadi Hassan Apr 22 '18 at 03:39
  • About returning the last matched element Instead of first - should be simple enough to adapt @CertainPerformance’s answer below. Why don’t you give it a shot? – Timir Apr 22 '18 at 03:55

7 Answers7

0

you can use indexOf function of Array object.

function checkArray(yourArray, value) {
  if (yourArray.indexOf(value) > -1) {
     // it means the value exists, so it has an index which is greater than -1.
     // action
  } else {
     return -1;
  }
}
amiraliamhh
  • 321
  • 2
  • 9
0

The trick to finding the last occurrence of an element is to start at the end (array.length-1) and iterate back towards the start using i--.

See return and for loop for more info.

// Index Of.
function indexOf(array, value) {
  for (var i = array.length-1; i >= 0; i --) {
    if (array[i] == value) return i
  }
  return -1
}

// Proof.
console.log(indexOf([ 0, 1, 3, 1, 2 ], 1))
console.log(indexOf([ 0, 1, 3, 1, 2 ], 2))
console.log(indexOf([ 0, 1, 3, 1, 2 ], 4))
console.log(indexOf([ 3, 3, 3 ], 3))
console.log(indexOf([], 5))
Arman Charan
  • 5,669
  • 2
  • 22
  • 32
0

You have

function IndexOf(array, value) {
  // ...
    } else if (value === undefined) {
      return -1;

So when the second argument is ever provided, -1 will never be returned. There also isn't much point pushing to an array and then immediately popping it and returning it - just return the i itself.

Just wait until all iterations are finished instead, and then return -1.

function IndexOf(array, value) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] === value) return i;
  }
  return -1;
}
console.log(IndexOf([0, 1, 3, 1, 2], 1));
console.log(IndexOf([0, 1, 3, 1, 2], 2));
console.log(IndexOf([0, 1, 3, 1, 2], 3));
console.log(IndexOf([3, 3, 3], 3));
console.log(IndexOf([], 5));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

The below code works for your purpose, where returning -1 is the only other return option if the value isn't found in the array.

function IndexOf (array,value) {
  var index = [];
  for (var i = 0; i < array.length; i++)
    if (array[i] === value) {
      index.push(i);
      return index.pop();
    }
  return -1
}

In your code, value will never equal undefined, rather value isn't located in the array and undefined is being returned due to lack of a return statement handling that condition.

Ari K
  • 434
  • 2
  • 18
0

Try this one. I hope this will work

function IndexOf(array, value) {
        var index = [];
        for (var i = 0; i < array.length; i++) {

            if (array[i] === value) {
                index.push(i);
            }
        }
        if(index.length > 0){
            return index.length;
        } else{
            return -1;
        }
    }
    console.log(IndexOf([0, 1, 3, 1, 2], 1));
    console.log(IndexOf([0, 1, 3, 1, 2], 2));
    console.log(IndexOf([0, 1, 3, 1, 2], 3));
    console.log(IndexOf([3, 3, 3], 3));
    console.log(IndexOf([], 5));
Vasanth
  • 443
  • 7
  • 16
0

Hopefully this snippet will be useful

function IndexOf(array, value) {
  // creating an empty array 
  var index = []; 
  for (var i = 0; i < array.length; i++)
    // if value matches return the index of the first matched element
    if (array[i] === value) {
      return i;
    } 
      return -1
    
}
console.log(IndexOf([0, 1, 3, 1, 2], 1));
console.log(IndexOf([0, 1, 3, 1, 2], 2));
console.log(IndexOf([0, 1, 3, 1, 2], 3));
console.log(IndexOf([3, 3, 3], 3));
console.log(IndexOf([], 5));
brk
  • 48,835
  • 10
  • 56
  • 78
  • Sorry I wasn't more clear. I need to return the last matched element as opposed to first. So output should be as followed: console.log(IndexOf([0, 1, 3, 1, 2], 1)); 3 console.log(IndexOf([0, 1, 3, 1, 2], 2)); 4 console.log(IndexOf([0, 1, 3, 1, 2], 3)); -1 console.log(IndexOf([3, 3, 3], 3)); 2 console.log(IndexOf([], 5)); -1 – Binny.H Apr 22 '18 at 03:48
0
    function IndexOf(array, value) {
      var match = -1;

      for (var i = 0; i < array.length; i++)
        if (array[i] === value)
          match = i;

      return match;
    }

console.log(IndexOf([3, 3, 3], 3)); // will return 2 as the last match index is 2
console.log(IndexOf([1,2,4], 5)); // will return -1 as there is no match

I guess this is what you are looking for.

Iftieaq
  • 1,904
  • 2
  • 15
  • 26