1

i have code in javascript like this, how to make it more easy to read, shorthand and efficient :

    var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

    var count = 0;
    for (i in numbers) {

        value = numbers[i];
        if (value % 2 == 0) {
            count++;
            if (count == 5) {
              console.log("The 5th even number is " + value);
            }
            if (count == 7) {
              console.log("The 7th even number is " + value);
            }
        }
    }

    count = 0;
    for (i in numbers) {
        value = numbers[i];
        if (value % 2 == 1) {
            count++;
            if (count == 3) {
              console.log("The 3rd odd number is " + value);
            }
            if (count == 8) {
              console.log("The 8th odd number is " + value);
            }
        }
    }
    var odds = [], evens = [];
    for (i in numbers) {
        value = numbers[i];
        if (value % 2 == 1) {
            odds.push(value);
        } else if (value % 2 == 0) {
            evens.push(value);`enter code here`
        }
    }

    console.log('The odd numbers: ' + odds.join(", "));
    console.log('The even numbers: ' + evens.join(", "));

and the result of those code just like this :

The 5th even number is 10 The 7th even number is 14 The 3rd odd number is 5 The 8th odd number is 15 The odd numbers: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 The even numbers: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20

4 Answers4

1

Instead of looping through the same array twice and checking for evens and odds you can just loop through once and keep your if statement for evens but and an else which will be all the odd numbers. At the same time as you go through this loop you can assign these numbers to your individual evens/odds arrays instead of looping through yet again. if you wanted to make it more dynamic you can add a function like below to get the suffix for the numbers for when you want to do your console log.

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
  ordinalSuffixOf = function(i) {
  var j = i % 10,
    k = i % 100;
  if (j == 1 && k != 11) {
    return i + "st";
  }
  if (j == 2 && k != 12) {
    return i + "nd";
  }
  if (j == 3 && k != 13) {
    return i + "rd";
  }
  return i + "th";
},
countEven = 0,
countOdd = 0,
odds = [],
evens = [];

for (i in numbers) {

  value = numbers[i];
  if (value % 2 == 0) {
    evens.push(value);
    countEven++;
    if (countEven == 5 || countEven == 7) {
      console.log("The " + countEven + ordinalSuffixOf(countEven) + " even number is " + value);
    }
  } else {
    odds.push(value);
    countOdd++;
    if (countOdd == 3 || countOdd == 8) {
      console.log("The " + countOdd + ordinalSuffixOf(countOdd) + " odd number is " + value);
    }
  }
}

console.log('The odd numbers: ' + odds.join(", "));
console.log('The even numbers: ' + evens.join(", "));
aconnelly
  • 662
  • 1
  • 6
  • 13
1
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

A lot of your "check for even" logic is repeated, so we can create a helper method to take care of it.

var isEven = function(number) {
  return number % 2 === 0;
};

We can use Array.prototype.filter to only get even numbers, then use Array.prototype.forEach to iterate through those even numbers. forEach() is even nice enough to keep track of which #th number we're on!

numbers.filter(isEven)
  .forEach(function(number, index) {
    if(index == 5 || index == 7) {
      console.log(`The ${index}th even number is ${number}`);
    }
  });

numbers.filter(!isEven)
  .forEach(function(number, index) {
    if(index == 3) {
      console.log(`The 3rd odd number is ${number}`);
    } else if(index == 8) {
      console.log(`The 8th odd number is ${number}`);
    }
  });

Again, using Array.prototype.filter, we don't have to fumble around with pushing things into an array.

var odds = numbers.filter(!isEven);
var evens = numbers.filter(isEven);

console.log(`The odd numbers: ${odds.join(', ')}`);
console.log(`The even numbers: ${evens.join(', ')}`);
therobinkim
  • 2,500
  • 14
  • 20
  • Very nice illustration of the advantages of using the newer "declarative" iterators over the old "imperative" C style `for (var i = 1; i <= 20; i++)`. Bravo. – user949300 Dec 21 '16 at 21:09
0

I'll give you a couple of tips.

First of all you could create a loop which adds numbers to your numbers array, instead of typing in the numbers you want manually.

var numbers = [];
for(var i = 1; i <= 20; i++){
    numbers.push(i);
}

Second of all, instead of this:

if (value % 2 == 1) {
    odds.push(value);
} else if (value % 2 == 0) {
    evens.push(value);`enter code here`
}

You could shorten it as such:

(value % 2 == 1) ? odds.push(value) : evens.push(value);

See this Stackoverflow answer for more insight into what these operators mean.

Community
  • 1
  • 1
Edwin Finch
  • 913
  • 3
  • 10
  • 24
0

since you are using a series of 1-20 number array, you can do it by single for loop, hope this code may help you

var evenCount = 0,
  oddCount = 0,
  odds = [],
  evens = [];
for (var i = 1; i <= 20; i++) {

  if (i % 2 == 0) {
    evenCount++;
    evens.push(i);
    if (evenCount == 5) {
      console.log("The 5th even number is " + i);
    }
    if (evenCount == 7) {
      console.log("The 7th even number is " + i);
    }
  } 
  else if (i % 2 == 1) {
    oddCount++;
    odds.push(i);
    if (oddCount == 3) {
      console.log("The 3rd odd number is " + i);
    }
    if (oddCount == 8) {
      console.log("The 8th odd number is " + i);
    }
  }
}



console.log('The odd numbers: ' + odds.join(", "));
  console.log('The even numbers: ' + evens.join(", "));
Azad
  • 5,144
  • 4
  • 28
  • 56