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

如何在索引循环中实现动态步长?特定跳步循环需求咨询

Solution to Modified Counting Loop

First, let's align with your example output pattern: after printing 9 consecutive numbers using a +1 step, you want to skip 5 numbers (like jumping from 9→15, 23→29) by switching to a +5 step once, then resume the +1 step for the next batch of 9 numbers. Here's the adjusted code:

let count = 0; // Tracks how many numbers we've output in the current sequence
for (let i = 1; i < 101; ) { // We'll handle step increments manually here
    if (count < 9) {
        console.log(i);
        count++;
        i += 1; // Normal +1 step when we're printing numbers
    } else {
        i += 5; // Jump 5 steps to skip the next 5 numbers (e.g., 10→15 skips 10-14)
        count = 0; // Reset count to start a new batch of outputs
    }
}

How This Logic Works:

  • The count variable keeps tabs on how many numbers we've printed since the last skip.
  • For each iteration:
    • If we haven't printed 9 numbers yet, we log the current value of i, increment the count, and move to the next number with i +=1.
    • Once we hit 9 printed numbers, we jump 5 steps forward (skipping the next 5 values), reset the count to 0, and start the cycle over.

Alternative Version (Matching Your Original Worded Requirement):

If your description ("每输出10个数后,将步长从i=i+1改为i=i+5") was the exact rule you want to follow (print 10 numbers then skip), here's that implementation:

let count = 0;
for (let i = 1; i < 101; ) {
    if (count < 10) {
        console.log(i);
        count++;
        i += 1;
    } else {
        i += 5;
        count = 0;
    }
}

This would output 1-10, jump to 15, output 15-24, jump to 29, and so on—strictly following the "10 outputs then +5 step" rule you mentioned.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:50:05