Created
August 16, 2020 16:02
-
-
Save Cihatata/66b4c876b6e9918575da17f15fcf1f09 to your computer and use it in GitHub Desktop.
Hacker Rank Plus Minus JS [SOLVED]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function plusMinus(arr) { | |
let positive = 0; | |
let negative = 0; | |
let zero = 0; | |
let len = arr.length; | |
arr.forEach((item) => { | |
if (item>0) { | |
positive++; | |
} else if (item<0) { | |
negative++; | |
} else { | |
zero ++; | |
} | |
}) | |
console.log((positive/len).toFixed(6) + "\n" + (negative/len).toFixed(6) + "\n" + (zero/len).toFixed(6)); | |
} | |
testArr = [1, -1, 3, -3, -4, 9, 12]; | |
plusMinus(testArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome