Quick actions

cmd+k|ctrl+k

Navigation

Languages

Plus & Minus

Snippet info

Language

JavaScript

Visibility

public

Author

krishnakanth

Created

2022-08-14T08:39:34.702326Z

Updated

2022-08-14T08:40:40.697447Z


function plusMinus(arr) {
    var Ps =0 ;
    var Ns =0 ;
    var Os =0 ;
    var l = arr.length ;
    for(let i=0;i<l;i++){
        var s = Math.sign(arr[i]);
        if(s==1){
            Ps++;
        }
        if(s==-1){
            Ns++
        }
        if(s==0){
           Os++ ;
        }
        
    }

    var array =[(Ps/l).toFixed(6) ,(Ns/l).toFixed(6),(Os/l).toFixed(6)];
    return array.join("/n") + "/n"
      
}
/**
 * 
 * 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.

Note: This challenge introduces precision problems. The test cases are scaled to six decimal places, though answers with absolute error of up to  are acceptable.

Example

There are  elements, two positive, two negative and one zero. Their ratios are ,  and . Results are printed as:*/
INFO