0

I am new to javascript and was trying this code below.

This code was working properly but seems now its only returning -1.

Array.prototype.nthIndexOf = function (element,location) {  
    var index = -1;
    for(var i=0; i< this.length; i++) {
        if(element === this[i] && !--location) {
            index = i;
            break;   
        }  
    }
    return index;
} 

var findNumber = prompt("Please enter number to be found");
var positionAt = prompt("Please enter position");

var position = [1, 2, 3, 3, 2, 89, 34, 12].nthIndexOf(findNumber, positionAt);
console.log(position); // position is -1 all the time
  • 1
    What is this function meant to do? Why does `positionAt` always have to equal `1` for the output to be different? – evolutionxbox Jul 05 '17 at 10:47
  • 3
    What's wrong with the [`Array.prototype.indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf)? – Washington Guedes Jul 05 '17 at 10:48
  • `prompt` returns a string but your array is containing numbers. – Thomas Jul 05 '17 at 10:48
  • @WashingtonGuedes :- I have tried hands on with indexOf and lastIndexOf but this function is required in a way in our company's application so using it like this. The array data will come in a different way. –  Jul 05 '17 at 10:57

3 Answers3

3

You got to use parseInt() for converting values you are getting from prompt to number as you are getting string value from prompt input.

Please read this page :- https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt

and this too :- difference between parseInt() and parseFloat()

Array.prototype.nthIndexOf = function (element,location) {  
    var index = -1;
    for(var i=0; i< this.length; i++) {
        if(element === this[i] && !--location) {
            index = i;
            break;   
        }  
    }
    return index;
} 

var findNumber = parseInt(prompt("Please enter number to be found"));
var positionAt = parseInt(prompt("Please enter position"));

// The type of operator will tell you type of object you are getting
// Remove parseInt above and check console log you will get string for findNumber type 
console.log(typeof findNumber);

var position = [1, 2, 3, 3, 2, 89, 34, 12].nthIndexOf(findNumber, positionAt);

if(position !== -1) {
    console.log(findNumber + " located at " + position);  
}else {
    console.log("Occurrence " + positionAt + " of number " + findNumber + " not found");
}
lalithkumar
  • 3,480
  • 4
  • 24
  • 40
Abhishek Sharma
  • 1,420
  • 1
  • 20
  • 30
0

Currently, it is comparing with the string value of prompt(). It always returns a string and you are also making a === strict comparison, including type. Use a parseInt() so that it correctly typecasts.

Array.prototype.nthIndexOf = function(element, location) {
  var index = -1;
  element = parseInt(element, 10);
  for (var i = 0; i < this.length; i++) {
    if (element === parseInt(this[i]) && !--location) {
      index = i;
      break;
    }
  }
  return index;
}

Snippet

Array.prototype.nthIndexOf = function(element, location) {
  var index = -1;
  element = parseInt(element, 10);
  for (var i = 0; i < this.length; i++) {
    if (element === parseInt(this[i]) && !--location) {
      index = i;
      break;
    }
  }
  return index;
}


var findNumber = prompt("Please enter number to be found");
var positionAt = prompt("Please enter position");

var position = [1, 2, 3, 3, 2, 89, 34, 12].nthIndexOf(findNumber, positionAt);
console.log(position); // position is -1 all the time

Instead of all these, you can use Array.prototype.indexOf!

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

you need to use Number() to change values from prompt to numbers. prompt always returns string values.

Array.prototype.nthIndexOf = function (element,location) {  
    var index = -1;
    for(var i=0; i< this.length; i++) {
        if(element === this[i] && !--location) {
            index = i;
            break;   
        }  
    }
    return index;
} 

var findNumber = prompt("Please enter number to be found");
var positionAt = prompt("Please enter position");

var position = [1, 2, 3, 3, 2, 89, 34, 12].nthIndexOf(Number(findNumber), Number(positionAt));
console.log(position); // position is -1 all the time
Dij
  • 9,761
  • 4
  • 18
  • 35