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

请求实现可循环跳转的数组元素跳过函数(附现有代码)

Fixing the Skip Function for Circular Array Navigation

Hey there! Let's tweak your existing skip function to handle circular wrapping when the index goes beyond the array's bounds. The core issue with your current code is that it doesn't account for indices falling outside the array's length—let's fix that with modulo arithmetic, which is perfect for creating circular patterns.

First, Let's Break Down the Original Code's Behavior

Your current function modifies the input pattern array by unshifting the start value, then accumulates an index to push elements. But when the index exceeds the array length (like in your example where idx hits 4 for an array of length 4), it tries to access arr[4] which returns undefined.

Optimized Function with Circular Wrapping

Here's the revised version that supports looping back to the start (or end, if you use negative steps):

function skip(start, pattern, arr) {
  // Handle edge case: empty array to avoid errors
  if (!arr.length) return [];
  
  let idx = 0;
  const res = [];
  // Combine start and pattern into steps without modifying the original pattern array
  const steps = [start, ...pattern];
  
  for (const step of steps) {
    idx += step;
    // Ensure index stays within array bounds (works for positive AND negative steps)
    idx = ((idx % arr.length) + arr.length) % arr.length;
    res.push(arr[idx]);
  }
  
  return res;
}

Key Improvements Explained

  • No side effects: Instead of pattern.unshift(start) (which alters the original pattern array), we create a new steps array using spread syntax. This keeps your input data intact.
  • Circular index handling: The line ((idx % arr.length) + arr.length) % arr.length ensures:
    • Positive indices that exceed the array length wrap back to the start (e.g., index 4 for an array of length 4 becomes 0).
    • Negative indices (if you ever use negative steps) wrap to the end of the array (e.g., index -1 becomes 3 for an array of length 4).
  • Empty array check: Prevents runtime errors if someone passes an empty array.

Testing Your Example

Let's run your sample call to see the difference:

skip(0, [1,1,2,1], [1, 2, 3, 4]);
  • Original output: [1, 2, 3, undefined, undefined]
  • New output: [1, 2, 3, 1, 2]

Which is exactly what you want—when the index hits 4 (after adding 2), it wraps back to 0 (the first element), then adding 1 takes it to index 1.

Another Test Case (Jump from the Last Item)

Let's verify the scenario you mentioned: jumping 2 times from the last item.

skip(3, [2], [1, 2, 3, 4]);
  • This starts at index 3 (value 4), then jumps 2 steps:
    1. First jump: index 3 + 1 = 0 (wraps around)
    2. Second jump: index 0 + 1 = 1
  • Output: [4, 2]—perfect, it loops back as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:36:07