【问题标题】:JavaScript: If argument is defined, function searches for index of where the argument appears in an arrayJavaScript:如果参数被定义,函数搜索参数出现在数组中的位置的索引
【发布时间】:2019-08-14 09:20:17
【问题描述】:

我尝试用JS解决下面的问题:

定义一个函数 myLastIndexOf,最多接受三个参数:

  • 数组
  • 搜索值
  • startIdx(可选)

  1. myLastIndexOf 应该返回 searchValue 在数组中出现的最后一个索引。

  2. 如果 searchValue 不在数组中,myLastIndexOf 应该返回 -1。

  3. 如果定义了 startIdx,myLastIndexOf 应该开始在该索引处查找 seachValue,然后在查找值时移向数组的前面。

这是我的代码:

function myLastIndexOf (array, searchValue, startIdx){
  if (startIdx !== undefined){
    for (i=startIdx; i>=0; i--){
      //console.log(array[i])
      if (array[i] === searchValue){
        return i
      }
    }
  }
  else if (startIdx === undefined){
    for (i=0; i<array.length; i++){
      console.log(array[i])
      if (array[i] === searchValue){
        return i
      }
    }
  }

}

console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee')); // => 3

代码应该返回 3,但我得到的却是 0。我的代码有什么问题?

【问题讨论】:

  • 你将在第一场比赛中回归
  • 该代码不返回 1,而是返回 0。

标签: javascript arrays loops search indexing


【解决方案1】:

您的循环从i=0 开始,它应该从i = array.length -1 开始。而i 应该减少而不是增加。

function myLastIndexOf (array, searchValue, startIdx){

  if (startIdx !== undefined){

    for (let i=startIdx; i>=0; i--){

      //console.log(array[i])

      if (array[i] === searchValue){
        return i
      }
    }
  }

  else if (startIdx === undefined){

    for (let i=array.length - 1; i>=0; i--){

      console.log(array[i])

      if (array[i] === searchValue){
        return i
      }
    }
  }
  return -1;

}




console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee'));

【讨论】:

  • 啊,你是对的!错过了我应该递减...谢谢:)
【解决方案2】:

这里有几个问题:

  1. 你在重复不需要的逻辑

  2. 您将返回 first 匹配而不是 last 匹配

  3. 你没有声明你的变量,所以你的代码会成为我所说的The Horror of Implicit Globals的牺牲品。

  4. 找不到匹配项时不会返回 -1

以下是有关 cmets 的更新:

function myLastIndexOf(array, searchValue, startIdx) {
  // If `startIdx` is undefined, use the end of the array
  if (startIdx === undefined) {
    startIdx = array.length - 1;
  }
  // Declare `i`
  var i;
  // Loop backward from `startIdx`
  for (i = startIdx; i >= 0; --i) {
    if (array[i] === searchValue) {
      // Stop the loop early
      break;
    }
  }
  return i; // Will be -1 if not found
}

console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee')); // => 3
console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee', 2)); // => 2, because of startIdx

循环实际上可以更短一些,但对于新手来说有点不太清楚:

// Loop backward from `startIdx`
while (i >= 0 && array[i] !== searchValue) {
    --i;
}

function myLastIndexOf(array, searchValue, startIdx) {
  // If `startIdx` is undefined, use the end of the array
  if (startIdx === undefined) {
    startIdx = array.length - 1;
  }
  // Declare `i`
  var i = startIdx;
  // Loop backward from `startIdx`
  while (i >= 0 && array[i] !== searchValue) {
    --i;
  }
  return i; // Will be -1 if not found
}

console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee')); // => 3
console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee', 2)); // => 2, because of startIdx

或使用 ES2015+ 功能:

function myLastIndexOf(array, searchValue, startIdx = array.length - 1) {
  let i;
  for (i = startIdx; i >= 0; --i) {
    if (array[i] === searchValue) {
      // Stop the loop early
      break;
    }
  }
  return i; // Will be -1 if not found
}

console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee')); // => 3
console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee', 2)); // => 2, because of startIdx

或者用更短的循环:

function myLastIndexOf(array, searchValue, startIdx = array.length - 1) {
  let i = startIdx;
  while (i >= 0 && array[i] !== searchValue) {
    --i;
  }
  return i; // Will be -1 if not found
}

console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee')); // => 3
console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee', 2)); // => 2, because of startIdx

【讨论】:

    【解决方案3】:

    您将在第一次匹配时返回值,因为您应该存储索引并在最后返回。

    function myLastIndexOf (array, searchValue, startIdx){
    let index = -1;
    if (startIdx !== undefined){
      for (i=startIdx; i>=0; i--){
        if (array[i] === searchValue){
          index = i
        }
      }
    }
    
    else if (startIdx === undefined){
      for (i=0; i<array.length; i++){
        if (array[i] === searchValue){
          index = i
        }
      }
    }
     return index
    }
    
    console.log(myLastIndexOf(['gee', 'gee', 'gee', 'gee', 'baby', 'baby'], 'gee'))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-30
      • 1970-01-01
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多