You are viewing a single comment's thread. Return to all 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));
}
Seems like cookies are disabled on this browser, please enable them to open this website
Plus Minus
You are viewing a single comment's thread. Return to all 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));
}