Plus Minus

  • + 0 comments

    JavaScript Solution:

    function plusMinus(arr) { const n = arr.length;

    if(n === 0){ console.log("0.000000"); console.log("0.000000"); console.log("0.000000"); return; }

    //count positive, negative, and zero elements

    const positiveCount = arr.filter(x => x > 0).length; const negativeCount = arr.filter(x => x < 0).length; const zeroCount = arr.filter(x => x === 0).length;

    //calculate ratios const positiveRatio = positiveCount / n; const negativeRatio = negativeCount / n; const zeroRatio = zeroCount / n;

    //print results with six decimal places

    console.log(positiveRatio.toFixed(6)); console.log(negativeRatio.toFixed(6)); console.log(zeroRatio.toFixed(6));

    }