user8255292
user8255292

Reputation:

Array function with number at position returning zero

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

Upvotes: 0

Views: 70

Answers (3)

Abhishek Sharma
Abhishek Sharma

Reputation: 1420

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");
}

Upvotes: 3

Dij
Dij

Reputation: 9808

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

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167212

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!

Upvotes: 0

Related Questions