数组函数,其位置返回零

时间:2017-07-05 10:46:07

标签: javascript

我是javascript的新手,正在尝试以下代码。

此代码工作正常,但现在似乎只返回-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

3 个答案:

答案 0 :(得分:3)

当你从提示输入中获取字符串值时,你必须使用parseInt()将你从提示获得的值转换为数字。

请阅读此页: - https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt

这也是: - difference between parseInt() and parseFloat()

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

答案 1 :(得分:0)

目前,它正在与prompt()的字符串值进行比较。它总是返回一个字符串,你也在进行===严格的比较,包括类型。使用parseInt()以便正确进行类型转换。

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

<强>段

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

您可以使用Array.prototype.indexOf

而不是所有这些

答案 2 :(得分:0)

您需要使用Number()将值从提示更改为数字。 prompt始终返回字符串值。

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