ProblemĀ from Hackerrank.
I was making the mistake of checking if the index was bigger or smaller than zero, instead of checking the value of the integer in that index. I was comparingĀ i
Ā instead ,arr[i]
.
My solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function plusMinus(arr) { let arrayLength = arr.length; let positiveFraction = 0; let negativeFraction = 0; let zeroFraction = 0; for ( let i = 0; i < arrayLength; i++) { if ( arr[i] > 0 ) { positiveFraction++; } else if ( arr[i] < 0 ) { negativeFraction++; } else { zeroFraction++; }; }; console.log(positiveFraction/arrayLength); console.log(negativeFraction/arrayLength); console.log(zeroFraction/arrayLength); }; |