admin管理员组

文章数量:1356926

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:

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!

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:

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!

Share Improve this question asked Feb 7, 2015 at 0:08 HectorOfTroy407HectorOfTroy407 1,9275 gold badges24 silver badges31 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3
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 plicated.

As for learning exercises, those typically work best when you solve them yourself.

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.

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.

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);
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 = []
    }
}

<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

本文标签: javascriptPrint even numbers from 1100 with 5 numbers per lineStack Overflow