首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >每5行重复跳过一次

每5行重复跳过一次
EN

Stack Overflow用户
提问于 2021-08-13 00:06:44
回答 3查看 43关注 0票数 1

我尝试在for循环中使用continue来跳过每5次迭代。但是,我只知道如何写continue,其中只需跳过第一个5。不确定如何实现continue语句才能使其工作。正确的输出应该是:

代码语言:javascript
复制
1  is odd.
2  is even.
3  is odd.
4  is even.
6  is even.
7  is odd.
8  is even.
9  is odd.
11  is odd.
12  is even.
13  is odd.
14  is even.
16  is even.
17  is odd.
18  is even.
19  is odd.

这是我的代码。

代码语言:javascript
复制
forwardOp(1,20)
function forwardOp(startIndex, endIndex){
    if (startIndex > endIndex){
        console.log("Start number cannot be less than End number.");
    }
    for (i = startIndex; i <= endIndex; i++){
        if (i === 5){continue;}
        if (i % 2 ==0){
            console.log (i," is even.")
        }else {
            console.log (i, " is odd.")
        }
    }
}
EN

Stack Overflow用户

回答已采纳

发布于 2021-08-13 00:18:20

您必须使用i % 5 == 0

为了简单起见,下面是修复后的代码:

代码语言:javascript
复制
forwardOp(1,20)
function forwardOp(startIndex, endIndex){
    if (startIndex > endIndex){
        console.log("Start number cannot be less than End number.");
    }
    for (i = startIndex; i <= endIndex; i++){
        if (i % 5 == 0){
            // 'i' is divisible by 5
        }
        if (i % 2 ==0){
            console.log (i," is even.")
        } else {
            console.log (i, " is odd.")
        }
    }
}
票数 2
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68765402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档