Returning an odd or even number from array

后端 未结 4 562
谎友^
谎友^ 2021-01-25 02:06

Just need help in identifying what I am doing wrong on this codewar challenge.

I realize this may be easy for some but please note I am just a beginner with Javascript.

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 02:45

    Another possible way:

    function myFunction(integers) {
      var odds = integers.filter(function(num) {return num % 2});
      var evens = integers.filter(function(num) {return !(num % 2)});
      return evens.length == 1 ? evens[0] : odds[0];
    }
    

    You can check out this CodePen Demo to test the function in Mocha.

提交回复
热议问题