Returning an odd or even number from array

后端 未结 4 640
谎友^
谎友^ 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:46

    If you want to get the first occurrence without iterating to the last item but without using an imperative code you may do as by using .find() checking inside if the rightmost bit of n is 1;

    var arr     = [2, 4, 0, 100, 4, 11, 2602, 36],
        resodd  = arr.find(n => n & 1),
        reseven = arr.find(n => !(n & 1));
    
    console.log(resodd, reseven);

提交回复
热议问题