DEV Community

Discussion on: Daily Challenge #35 - Find the Outlier

Collapse
 
ra9 profile image
Carlos Nah • Edited

Here's my code:
I believe I could have used filters on the arrays method but just decided to follow the long step.



  const findOutlier = (arr) => {
    const evens = [];
    const odds = [];
    for(let i in arr) {
        const isEven = arr[i] % 2 === 0;
        const isOdd = !isEven;
        if(isEven){
            evens.push(arr[i]);
        }
        if(isOdd) {
            odds.push(arr[i])
        } 
    }

    if(odds.length === 0 || evens.length === 0) {
        return null;
    }

    return  odds.length  < evens.length ? odds[0] : evens[0];
  }

  console.log([
      [160, 3, 1719, 19, 11, 13, -21],
      [4, 8, 15, 16, 24, 42],
      [16, 6, 40, 66, 68, 28]
  ].map(findOutlier));

Here's my code:
I believe I could have used filters on the arrays method but just decided to follow the long step.



  const findOutlier = (arr) => {
    const evens = [];
    const odds = [];
    for(let i in arr) {
        const isEven = arr[i] % 2 === 0;
        const isOdd = !isEven;
        if(isEven){
            evens.push(arr[i]);
        }
        if(isOdd) {
            odds.push(arr[i])
        } 
    }

    if(odds.length === 0 || evens.length === 0) {
        return null;
    }

    return  odds.length  < evens.length ? odds[0] : evens[0];
  }

  console.log([
      [160, 3, 1719, 19, 11, 13, -21],
      [4, 8, 15, 16, 24, 42],
      [16, 6, 40, 66, 68, 28]
  ].map(findOutlier));

Here's my code:
I believe I could have used filters on the arrays method but just decided to follow the long step.



  const findOutlier = (arr) => {
    const evens = [];
    const odds = [];
    for(let i in arr) {
        const isEven = arr[i] % 2 === 0;
        const isOdd = !isEven;
        if(isEven){
            evens.push(arr[i]);
        }
        if(isOdd) {
            odds.push(arr[i])
        } 
    }

    if(odds.length === 0 || evens.length === 0) {
        return null;
    }

    return  odds.length  < evens.length ? odds[0] : evens[0];
  }

  console.log([
      [160, 3, 1719, 19, 11, 13, -21],
      [4, 8, 15, 16, 24, 42],
      [16, 6, 40, 66, 68, 28]
  ].map(findOutlier));