简体   繁体   English

我有一个打印数字的 for 循环。 但我不想它打印几个数字

[英]I have a for loop that print's numbers. But I don't want it to print a few numbers

So i have a for loop that prints numbers from 3333 to 9999 in JavaScript .所以我有一个 for 循环,它在JavaScript中打印从 3333 到 9999 的数字。 I want it so that when my for loop prints the numbers, it checks for every number if it contains the number 0, 1, 2 or 5. This is the code I have written till now but it still doesn't work.我想要这样,当我的 for 循环打印数字时,它会检查每个数字是否包含数字 0、1、2 或 5。这是我迄今为止编写的代码,但它仍然不起作用。 Hope you can help me.希望您能够帮助我。

    for (let i = 3333; i <= 9999; i++) {
        if (i === 1) {
            continue;
        } else if (i === 0) {
            continue;
        } else if (i === 5) {
            continue;
        } else if (i === 2) {
            continue;
        } else {
            console.log(i)
        }

    }
}

printNum();

A solution with regex would look like this使用正则表达式的解决方案如下所示

 for (let i = 3333; i <= 9999; i++) { if (/[0125]/.test(i)) { continue; } else { console.log(i) } }

for (let i = 3333; i <= 9999; i++) {
    if (i.toString().includes(0) || i.toString().includes(1) ||i.toString().includes(2) || i.toString().includes(5)) {
        continue;
    } else {
        console.log(i)
    }
}

the following code will work fine.以下代码将正常工作。 It converts the number to a string and checks if it contains 0,1,2 or 5 and if it finds such a number it just continues without printing it to the console它将数字转换为字符串并检查它是否包含 0、1、2 或 5,如果找到这样的数字,它会继续而不将其打印到控制台

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不想打印#登录网址 - I don't want to print # sign in url 我只想在 Javascript 中使用 Arrays 打印 1 到 100 个数字 - I Want To Print 1 to 100 Numbers Using Arrays In Javascript Only 当我不想时,变量在高数字上增加1 - Variable increases by 1 on high numbers when I don't want to 我正在创建一个数组以响应打印图像 select 仅基于数字我想要的图像 - i am create an array in react to print images select only images i want based on to numbers 我有一个数字数组。 问题是从中获取一组独特的对象 - I have an array of numbers. the issue is to get an array of unique objects from this 在新行上使用For循环打印数字 - Print Numbers with For Loop on New Lines 我想生成直到 3 的随机数,但我不想重复它们。 我怎样才能让它工作? - I want to generate random numbers till 3 but i don't want to repeat them. How can i make it work? 我想在AngularJS应用程序中创建一个输入数字框,用户不能在该输入框上键入十进制数字。 (整数输入框) - I want to create an input number box in AngularJS app on which a user should not be able to type decimal numbers. (an integer number input box) 我有多个图像路径的数组,我想使用循环打印图像 - I have array with multiple images path and i want to print images using loop 我正在尝试使用 Javascript 中的 while 循环打印从 10 到 40 的所有偶数 - I'm trying to print all even numbers from 10 to 40 using just a while loop in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM