[英]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.