[英]How to return an object rather than console log values
I wrote a JavaScript function to find the number of odd integers, number of negative integers, average, and median value. 我编写了一个JavaScript函数来查找奇数整数,负整数数,平均值和中位数。
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 )
我还包括了指向我的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]);
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);
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]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.