javascript 打印 1-100 的偶数,每行 5 个数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28376903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print even numbers from 1-100 with 5 numbers per line
提问by HectorOfTroy407
trying to brush up on some basic javascript and I'm wondering how you would solve this. I found a way but it's pretty ugly and I'd appreciate some more experienced eyes letting me know what they thing. Basically, just iterate over the numbers 1-100 and print all even numbers in groups of five on each line. So line one would be 2,4,5,8,10, line 2 is 12,14,16,18,20. Here is what I have so far:
试图复习一些基本的 javascript,我想知道你将如何解决这个问题。我找到了一种方法,但它非常丑陋,我很感激一些更有经验的眼睛让我知道他们在做什么。基本上,只需遍历数字 1-100 并在每行上以五个为一组打印所有偶数。所以第一行是 2,4,5,8,10,第二行是 12,14,16,18,20。这是我到目前为止所拥有的:
var counter = 0;
var line=[];
for(var i = 0; i<=100; i++){
if(i%2==0){
if(line.length <5){
line[counter] = i;
counter++
}else{
console.log(line);
line=[];
counter=0;
line[counter] =i;
}
}
}
console.log(line);
Thanks so much!
非常感谢!
回答by coreyward
console.log(2,4,6,8,10);
console.log(12,14,16,18,20);
console.log(22,24,26,28,30);
console.log(32,34,36,38,40);
console.log(42,44,46,48,50);
console.log(52,54,56,58,60);
console.log(62,64,66,68,70);
console.log(72,74,76,78,80);
console.log(82,84,86,88,90);
console.log(92,94,96,98,100);
With most hypothetical programming problems, it's most worthwhile to address their lack of rooting in reality and do what is simplest —?reality is sure to prove to be much more complicated.
对于大多数假设的编程问题,最值得解决的是它们缺乏扎根于现实的问题,并做最简单的事情——事实肯定会证明要复杂得多。
As for learning exercises, those typically work best when you solve them yourself.
至于学习练习,当你自己解决它们时,这些练习通常效果最好。
回答by Adrian Stoll
var line = [];
//you are uninterested in odd numbers, so increment the counter by 2
for(var i = 0; i <= 1000; i = i + 2) {
line.push(i);
if((i + 2) % 5 === 0) { //every 5 lines print result
console.log(line);
line = []
}
}
回答by Guffa
The problems that I see with the code is that you are looping from 0 instead of 1, and that you are not increasing the counter in the else
block.
我在代码中看到的问题是您从 0 而不是 1 循环,并且您没有增加else
块中的计数器。
Fixing that you get:
修复你得到的:
var counter = 0;
var line=[];
for (var i = 1; i <= 100; i++) {
if (i % 2 == 0){
if (line.length < 5) {
line[counter] = i;
counter++
} else {
console.log(line);
line=[];
counter = 0;
line[counter] = i;
counter++
}
}
}
console.log(line);
Instead of looping from 1 to 100 and checking if the number is even, just loop from 2 to 100 in steps of two. You don't need the counter at all, you can push the items into the array. Instead of repeating the code that adds an item to the array in the if
and else
blocks you can just do it once after.
不是从 1 到 100 循环并检查数字是否为偶数,而是以两步从 2 到 100 循环。您根本不需要计数器,您可以将项目推入数组。无需重复将项目添加到if
和else
块中的数组的代码,您只需在此之后执行一次即可。
With those simplifications you get:
通过这些简化,您将得到:
var line=[];
for (var i = 2; i <= 100; i += 2) {
if (line.length == 5) {
console.log(line);
line=[];
}
line.push(i);
}
console.log(line);
回答by SunilK
<script>
var counter = 0;
var line = [];
for (var i = 2; i <= 100; i += 2) {
line[counter] = i;
counter++;
if (line.length == 5) {
console.log(line);
counter = 0;
line = [];
}
}
</script>
Same as yours, but use a loop incrementing by 2
与您的相同,但使用递增 2 的循环