While working on coding to round numbers to six decimal places after performing some arithmetic operations, I encountered a problem. I was iterating through the elements of an array and conducting calculations based on the array contents. To achieve rounding to six decimal places, I used the toFixed function with a parameter of 6. However, when dividing the array contents by the array length and applying toFixed(6), I noticed that only one decimal place was being displayed instead of six.
array = [1, 1, 0, -1, -1];
var positive_count = 0;
var negative_count = 0;
var zero_count = 0;
function plusMinus(array) {
for(var i = 0; i < array.length; i++) {
if(array[i] > 0) {
positive_count++;
} else if (array[i] < 0) {
negative_count++;
} else if (array[i] == 0) {
zero_count++;
}
}
var calculatePos = positive_count/array.length.toFixed(6);
calculatePos.toFixed(6);
console.log(calculatePos);
var calculateNeg = negative_count/array.length.toFixed(6);
console.log(calculateNeg);
var calculateZero = zero_count/array.length.toFixed(6);
console.log(calculateZero);
}
plusMinus(array);