Skip to content

Instantly share code, notes, and snippets.

@Cihatata
Created August 16, 2020 16:02
Show Gist options
  • Save Cihatata/66b4c876b6e9918575da17f15fcf1f09 to your computer and use it in GitHub Desktop.
Save Cihatata/66b4c876b6e9918575da17f15fcf1f09 to your computer and use it in GitHub Desktop.
Hacker Rank Plus Minus JS [SOLVED]
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);
@ayowilfred95
Copy link

This is awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment