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

4
  • 1
    What is this function meant to do? Why does positionAt always have to equal 1 for the output to be different? Commented Jul 5, 2017 at 10:47
  • 3
    What's wrong with the Array.prototype.indexOf()? Commented Jul 5, 2017 at 10:48
  • prompt returns a string but your array is containing numbers. Commented Jul 5, 2017 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. Commented Jul 5, 2017 at 10:57

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Link only answers are frowned upon here. I hope you knew it before.
It is better to put your comments /* ... */ out of the snippet. People may not like snippet-only answers (even with the comments inside it)
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!

Comments

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

Comments

Your Answer

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