My goal is to create a JavaScript function that takes a list of numbers as input and outputs the count of odd numbers, negative numbers, calculates the average, and determines the median. I believe I have completed the code, but I am struggling with syntax issues or incorrect return statements.
Here's the code:
var arrayAnalyze = function(numbers){
var oddNum = []; //Array to store odd numbers
var negNum = []; //Array to store negative numbers
var numSum = 0; // Sum of all numbers
var avgNum = 0; //Average of all numbers
var midNum = []; //Median number
//Store odd numbers in oddNum array
for (i = 0; i < numbers.length; i++){
if (numbers[i] % 2 !== 0){
oddNum.push(numbers[i]);
}
}
//Store negative numbers in negNum array
for (i = 0; i < numbers.length; i++){
if (Math.abs(numbers[i]) + numbers[i] === 0){
negNum.push(numbers[i]);
}
}
//Calculate sum of all numbers
for (i = 0; i < numbers.length; i++){
numSum += i;
}
//Calculate average of numbers
avgNum = numSum / numbers.length;
//Calculate median of numbers
numbers.sort(function(a,b){return a - b;});
var evenSplit = Math.floor(numbers.length / 2);
if(numbers.length % 2){
midNum = numbers[evenSplit];
}else{
midNum = (numbers[evenSplit - 1] + numbers[evenSplit]) / 2.0; }
midNum.push();
return "Odds: " + oddNum.length, "Negatives: " + negNum.length, "Average: " + avgNum.toFixed(2), "Median: " + midNum[0];
};
console.log(arrayAnalyze([7, -3, 0, 12, 44, -5, 3]));
Output:
TypeError: numbers.sort is not a function