How many Odd and Even number are the in a array

后端 未结 3 513
抹茶落季
抹茶落季 2020-12-20 10:52

I have created an Array with some numbers. I want to find out how many even, and how many odd numbers it is in this Array. I have to print it out like this: (this is j

3条回答
  •  时光说笑
    2020-12-20 11:09

    You could iterate with Array#reduce and count only the odds. For the rest just take the difference of the length of the array and the odds.

    var tall = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
        odd = tall.reduce(function (r, a) { return r + a % 2; }, 0),
        even = tall.length - odd;
    
    console.log('odd', odd);
    console.log('even', even);

提交回复
热议问题