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

deleteArrayElements函数实现异常:调用结果与预期不符求调试

问题排查:deleteArrayElements函数逻辑错误

需求回顾

  • 实现deleteArrayElements()函数,从数组指定起始索引startIndex开始读取number个元素,删除该子数组中每隔everyIth个的元素
  • startIndex可大于数组长度,需支持循环寻址
  • everyIth可大于数组长度,但至少要删除子数组的第0个元素
  • 函数需返回删除后的数组及被删除元素列表
  • 示例调用:deleteArrayElements(number = 6, startIndex = 0, everyIth =2),需删除传入数组前6个元素中的每隔一个元素

现有代码

function deleteArrayElements(number, startIndex, everyIth) {
  let array = [];  
  let result = [];
  let removedItems = [];

  if (startIndex > array.length) {
    startIndex = startIndex % array.length;
  }

  for (let i = startIndex; i < startIndex + number; i += everyIth) {
    let indexToRemove = i % array.length;
    removedItems.push(array[indexToRemove]);
    array.splice(indexToRemove, 1);
  }

  result = array;
  return { newResult: result, removedItems: removedItems };
}

问题现象

  • 预期输出:{"newResult":[null, "katze", null, "elefant", null, "stachelschwein", "affe", "giraffe"], "removedItems":["hund", "maus", "schlange"]}
  • 实际输出:{"newResult":["hund", "katze", "maus", "elefant", "schlange", "stachelschwein", "affe", "giraffe"], "removedItems":["hund", "elefant", "affe"]}

错误分析

  1. 缺失输入数组参数:函数未定义原始数组输入参数,直接初始化空数组array = [],完全脱离实际业务场景,这是核心错误。
  2. 循环寻址逻辑不严谨:仅在startIndex > array.length时取模,未考虑数组长度为0的异常情况,且正确的循环寻址应直接用startIndex % array.length统一处理所有索引超出范围的情况。
  3. 子数组范围处理错误:需求是先从起始索引取number个元素组成子数组,再在子数组内删除间隔元素;但原代码直接在原数组上按固定间隔取模删除,完全偏离需求逻辑。
  4. splice操作引发索引偏移:每次splice删除元素后,数组后续元素索引会前移,但原代码仍使用原始计算的索引,导致删除错误的元素。

修正后的代码

function deleteArrayElements(originalArray, number, startIndex, everyIth) {
  // 复制原数组避免修改原始数据
  let array = [...originalArray];
  let removedItems = [];
  const arrLength = array.length;

  if (arrLength === 0) {
    return { newResult: [], removedItems: [] };
  }

  // 统一处理循环寻址,支持正负索引
  startIndex = startIndex % arrLength;
  if (startIndex < 0) {
    startIndex += arrLength;
  }

  // 第一步:确定要操作的子数组的所有索引(循环取number个元素)
  const subArrayIndices = [];
  for (let i = 0; i < number; i++) {
    subArrayIndices.push((startIndex + i) % arrLength);
  }

  // 第二步:找出子数组中需要删除的元素位置(每隔everyIth个,从0开始)
  const removePositions = [];
  for (let i = 0; i < subArrayIndices.length; i += everyIth) {
    removePositions.push(i);
  }

  // 第三步:映射回原数组索引,按降序排列后删除,避免索引偏移
  const originalRemoveIndices = removePositions.map(pos => subArrayIndices[pos]).sort((a, b) => b - a);
  
  originalRemoveIndices.forEach(index => {
    removedItems.push(array.splice(index, 1)[0]);
  });

  // 恢复删除元素的顺序(因从后往前删除,需反转数组)
  removedItems.reverse();

  return { newResult: array, removedItems: removedItems };
}

验证说明

假设输入原始数组为["hund", null, "katze", "maus", null, "elefant", "schlange", null, "stachelschwein", "affe", "giraffe"],调用deleteArrayElements(originalArray, 6, 0, 2),将得到预期输出:

  • newResult:[null, "katze", null, "elefant", null, "stachelschwein", "affe", "giraffe"]
  • removedItems:["hund", "maus", "schlange"]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 00:54:54