You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript多维数组首元素查找问题及splice()用法疑惑

关于findFirstMove函数的逻辑错误与splice用法解析

我是编程初学者,正在完成Rithm School的findFirstMove编程练习时遇到问题。题目要求实现一个findFirstMove函数,接收多维moves数组和一个move字符,返回该字符首次出现的索引数组,不存在则返回-1。

我仅掌握基础数组方法(如splice、slice等),编写的代码在本地VS Code的Chrome控制台中测试数组返回结果符合预期,但在Jasmine测试套件中却失败。修改返回值为test变量时测试通过,但本地控制台结果又不符合预期。我对自己代码的逻辑错误及splice()的用法存在疑惑,希望得到解析。

我的代码

function findFirstMove(moves, char) {
    let newArr = []
    for(let i=0; i<moves.length; i++)
    {
        for(let j=0; j<moves[i].length; j++)
        {
            if(moves[i][j] === char)
            {
                 newArr.push(i,j)
            }
        }
    }

    let test = []
    if(newArr.length)
    {
        test = newArr.splice(newArr.length - 2, 2)
        return newArr
    }
    else
    {
        return -1
    }
    
}

测试数组

let moves = [
    ["a", "b", "c"],
    ["d", "a", "f"],
    ["g", "h", "h"],
  ];

预期输出

console.log(findFirstMove(moves, "a")); // [0, 0]
console.log(findFirstMove(moves, "h")); // [2, 1]

Jasmine测试用例

describe("findFirstMove", function () {
  it("should return the index of the first move in the array of arrays of moves", function () {
    const moves = [
      ["a", "b", "c"],
      ["d", "e", "f"],
      ["g", "h", "i"],
    ];
    const move = "a";
    const result = findFirstMove(moves, move);
    expect(result).toEqual([0, 0]);
  });
  it("should return the index of the first move in the array of arrays of moves", function () {
    const moves = [
      ["a", "b", "c"],
      ["d", "e", "f"],
      ["g", "h", "i"],
    ];
    const move = "h";
    const result = findFirstMove(moves, move);
    expect(result).toEqual([2, 1]);
  });
  it("should return the index of the first move in the array of arrays of moves", function () {
    const moves = [
      ["a", "b", "c"],
      ["d", "e", "f"],
      ["g", "h", "i"],
    ];
    const move = "z";
    const result = findFirstMove(moves, move);
    expect(result).toEqual(-1);
  });
});

正确解法

function findFirstMove(moves, char) {
    for(let i=0; i<moves.length; i++)
    {
        for(let j=0; j<moves[i].length; j++)
        {
            if(moves[i][j] === char)
            {
                 return [i,j]
            }
        }
    }
    return -1
}

错误解析

1. 核心逻辑偏离需求

题目要求返回首次出现的索引,但你的代码会遍历整个二维数组,把所有匹配字符的索引都存入newArr,最后取数组中前半部分返回。这在本地测试数组中只是巧合符合预期:比如找"a"时,newArr是[0,0,1,1],splice删除最后两个元素后,newArr变成[0,0],刚好是首次出现的索引;但如果换一个场景,比如字符首次出现在中间位置,后续还有匹配项,你的代码就会返回错误结果。

2. 对splice方法的误解

Array.splice(start, deleteCount)的作用是:

  • 直接修改原数组,删除从start索引开始的deleteCount个元素
  • 返回被删除的元素组成的新数组

你的代码中:

test = newArr.splice(newArr.length - 2, 2)
return newArr

当newArr包含多组索引时,splice会删除最后一组索引,返回给test,而你返回的是被修改后的newArr(即前面所有匹配的索引)。这在本地测试中刚好巧合得到正确结果,但逻辑完全错误。如果改成返回test,则会返回最后一次出现的索引,与题目要求的首次出现完全相反。

3. 正确逻辑的核心

正确解法的关键是找到匹配项立即返回:一旦在遍历中找到目标字符,直接返回当前的[i,j],终止后续遍历,这样就能保证返回的是首次出现的索引。如果遍历完整个数组都没有找到匹配项,再返回-1。

内容的提问来源于stack exchange,提问作者Kruseverve

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:32:45