-2

I was trying to solve this problem on hackerrank.

Description: 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 6 places after the decimal.

Problem: I tried my code but it counts negative numbers as positive ones

Here is my code:

function plusMinus(arr) {
// Write your code here
let positive=0;
let zero=0;
let negative=0;
for (let i=0;i<arr.length;i++){
    if (i<0){
        minus+=1;
    }
    else if(i>0){
        positive+=1
    }
    else{
        zero+=1
    }
}

const posRatio=positive/arr.length;
const negRatio=minus/arr.length;
const zeroRatio=zero/arr.length;

console.log(posRatio,negRatio,zeroRatio)

}

Sherif
  • 17
  • 4

0 Answers0