Advertisement
CLooker

Untitled

Feb 2nd, 2018
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://www.hackerrank.com/challenges/plus-minus/problem
  2.  
  3.  
  4. function plusMinus(arr) {
  5.     const plusMinusCount = arr.reduce((plusMinusCount, n) => {
  6.         if (n > 0) {
  7.             plusMinusCount.posCount = plusMinusCount.posCount + 1;
  8.         }
  9.         else if (n < 0) {
  10.             plusMinusCount.negCount = plusMinusCount.negCount + 1;      
  11.         } else {
  12.             plusMinusCount.zeroCount = plusMinusCount.zeroCount + 1;
  13.         }
  14.         return plusMinusCount;
  15.     }, {posCount: 0, negCount: 0, zeroCount: 0});
  16.     console.log(plusMinusCount.posCount/arr.length);
  17.     console.log(plusMinusCount.negCount/arr.length);
  18.     console.log(plusMinusCount.zeroCount/arr.length);
  19. }
  20.  
  21. function plusMinus(arr) {
  22.     const plusMinusCount = arr.reduce(({ posCount, negCount, zeroCount }, n) => (
  23.         n > 0
  24.           ? {posCount: ++posCount, negCount, zeroCount}
  25.           : n < 0
  26.             ? {posCount, negCount: ++negCount, zeroCount}
  27.             : {posCount, negCount, zeroCount: ++zeroCount}
  28.     ), {posCount: 0, negCount: 0, zeroCount: 0});
  29.     console.log(plusMinusCount.posCount/arr.length);
  30.     console.log(plusMinusCount.negCount/arr.length);
  31.     console.log(plusMinusCount.zeroCount/arr.length);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement