Skip to content

Commit 0d5e173

Browse files
author
wakidurrahman
committed
feat: plusMinus problem solved
1 parent 3d14df9 commit 0d5e173

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/hackerrank/problem-solving/algorithms-004-010.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function aVeryBigSum(ar) {
1515
return sum;
1616
}
1717

18-
// 004:
18+
// 004: Diagonal Difference
1919

2020
function diagonalDifference(arr) {
2121
const lengthOfArray = arr.length; // Length of the array
@@ -46,3 +46,32 @@ const myArray = [
4646
[4, 5, 6, 7],
4747
];
4848
diagonalDifference(myArray);
49+
50+
// 005: Given an array of integers, calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.
51+
/*
52+
* Complete the 'plusMinus' function below.
53+
*
54+
* The function accepts INTEGER_ARRAY arr as parameter.
55+
*/
56+
57+
function plusMinus(arr) {
58+
const arrLength = arr.length;
59+
let positiveCount = 0;
60+
let negativeCount = 0;
61+
let zeroCount = 0;
62+
for (let i = 0; i < arrLength; i++) {
63+
if (arr[i] === 0) {
64+
zeroCount += 1;
65+
} else if (arr[i] > 0) {
66+
positiveCount += 1;
67+
} else if (arr[i] < 0) {
68+
negativeCount += 1;
69+
}
70+
}
71+
const calculatePos = (positiveCount / arrLength).toFixed(6);
72+
const calculateNeg = (negativeCount / arrLength).toFixed(6);
73+
const calculateZero = (zeroCount / arrLength).toFixed(6);
74+
console.log(calculatePos + '\n' + calculateNeg + '\n' + calculateZero);
75+
}
76+
arrayOfIntegers = [-4, 3, -9, 0, 4, 1];
77+
plusMinus(arrayOfIntegers);

0 commit comments

Comments
 (0)