Reputation: 45
I wrote a JavaScript function to find the number of odd integers, number of negative integers, average, and median value.
The code is accomplishing everything that I wanted it to, however, I am wondering if I can rewrite it to get the function to return an object rather than just the console log values. I've also included a link to my JS Bin (https://jsbin.com/digagozuru/edit?html,css,js,console,output)
Any advice or suggestions would be appreciated! Thanks.
var arrayAnalyzer = function(myArray) {
var odds = 0;
var negatives = 0;
var avg;
var median;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 !== 0) {
odds += 1;
}
if (myArray[i] < 0) {
negatives += 1;
}
}
console.log("There are " + odds + " odd numbers.");
console.log("There are " + negatives + " negative numbers.");
var sum = myArray.reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
});
avg = sum / myArray.length;
console.log("The average is " + avg.toFixed(2));
var orderedArray = myArray.sort(function(a, b) {
return a - b;
});
if (orderedArray.length % 2 === 0) {
var position1 = orderedArray.length / 2;
var position2 = position1 - 1;
median = (orderedArray[position1] + orderedArray[position2]) / 2;
} else {
var position = Math.floor(orderedArray.length / 2);
median = orderedArray[position];
}
console.log("The median is " + median);
};
arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]);
Upvotes: 0
Views: 88
Reputation: 1219
var arrayAnalyzer = function(myArray) {
var odds = 0;
var negatives = 0;
var avg;
var median;
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 !== 0) {
odds += 1;
}
if (myArray[i] < 0) {
negatives += 1;
}
}
console.log("There are " + odds + " odd numbers.");
console.log("There are " + negatives + " negative numbers.");
var sum = myArray.reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
});
avg = sum / myArray.length;
console.log("The average is " + avg.toFixed(2));
var orderedArray = myArray.sort(function(a, b) {
return a - b;
});
if (orderedArray.length % 2 === 0) {
var position1 = orderedArray.length / 2;
var position2 = position1 - 1;
median = (orderedArray[position1] + orderedArray[position2]) / 2;
} else {
var position = Math.floor(orderedArray.length / 2);
median = orderedArray[position];
}
console.log("The median is " + median);
// Returns an object with named attributes
return {
odds:odds,
negatives:negatives,
avg:avg,
median:median
};
};
var myArray = arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]);
console.log("Odds: " + myArray.odds +
"\nNegatives: " + myArray.negatives +
"\nAverage:" + myArray.avg +
"\nMedian: " + myArray.median);
Upvotes: 2
Reputation: 1993
I'm not sure if I get your question right, but why dont create an object at the beginning of your method and then write the values in the object as soon as you calculated them?
var arrayAnalyzer = function(myArray) {
var odds = 0;
var negatives = 0;
var avg;
var median;
var result = {};
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] % 2 !== 0) {
odds += 1;
}
if (myArray[i] < 0) {
negatives += 1;
}
}
result.negatives = negatives;
result.odds = odds;
var sum = myArray.reduce(function(previousValue, currentValue) {
return previousValue + currentValue;
});
avg = sum / myArray.length;
result.avg = avg;
var orderedArray = myArray.sort(function(a, b) {
return a - b;
});
if (orderedArray.length % 2 === 0) {
var position1 = orderedArray.length / 2;
var position2 = position1 - 1;
median = (orderedArray[position1] + orderedArray[position2]) / 2;
} else {
var position = Math.floor(orderedArray.length / 2);
median = orderedArray[position];
}
result.median = median;
return result;
};
console.log(arrayAnalyzer([7, -3, 0, 12, 44, -5, 3]));
Upvotes: 1