[英]how to find even numbers?
I am trying to find even numbers from 0 to 100 but it is not working.我试图找到从 0 到 100 的偶数,但它不起作用。 even it not showing any error.即使它没有显示任何错误。 can anyone help me to figure out?谁能帮我弄清楚?
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var x = 1;
while (x <= 100) {
if (x % 2 == 0) {
console.log(x);
} else {
break;
}
x++;
}
</script>
</body>
</html>
for(let x =0; x<=100; x+=2){ console.log(x); }
I wouldn't check for even numbers.我不会检查偶数。 It's easier to just start by 0 and add 2 each iteration.从 0 开始并在每次迭代中添加 2 会更容易。
function printEven() {
for (let i = 1; i < 100; i++) {
if(i % 2 === 0) {
console.log(i);
}
}
}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var x = 1;
while (x <= 100) {
if (x % 2 == 0) {
console.log(x);
} else {
break;
}
x++;
}
</script>
</body>
</html>
It stops after the firs round, because "if (x % 2 == 0)" does not match and else is a "break"它在第一轮后停止,因为“if (x % 2 == 0)”不匹配,否则是“break”
It breaks out of your while loop like ivar said它打破了你的 while 循环,就像 ivar 说的那样
while (x <= 100) {
if (x % 2 == 0) {
console.log(x);
}
x++;
}
Here as a one liner:这里作为一个班轮:
Array(50).fill().map((_, i) => i*2+2).forEach((v) => console.log(v)) // Alternatives console.log( Array(50).fill().map((_, i) => i*2+2) ) console.log( [...Array(50).keys()].map(i => i*2+2) ) console.log( [...Array(50)].map((_, i) => i*2+2) ) console.log( Array.from(Array(50), (_, i) => i*2+2) )
jk jk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.