Như những người khác đã nói, đây là mã hợp lý cho người mới bắt đầu. Và câu trả lời của @Sacho đưa ra những gợi ý tốt để cải thiện nó
Dưới đây là giải pháp sử dụng một số kỹ thuật nâng cao hơn một chút, nếu bạn quan tâm
var mode = function mode[arr] {
return arr.reduce[function[current, item] {
var val = current.numMapping[item] = [current.numMapping[item] || 0] + 1;
if [val > current.greatestFreq] {
current.greatestFreq = val;
current.mode = item;
}
return current;
}, {mode: null, greatestFreq: -Infinity, numMapping: {}}].mode;
};
console.log[mode[[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6]]];
//=> 3
Để xem nó hoạt động như thế nào, nếu bạn đánh rơi .mode
ở cuối hàng, bạn sẽ nhận được đồ vật này thay thế
{
"mode": 3,
"greatestFreq": 4,
"numMapping": {"1": 2, "2": 2, "3": 4, "4": 2,
"5": 3, "6": 2, "7": 1, "8": 2, "9": 3}
}
Đối tượng này được xây dựng từng phần một bằng lệnh gọi reduce
chạy trên từng phần tử của mảng, cập nhật numMapping
mỗi lần và greatestFreq
và mode
khi thích hợp
//
// Calculating the average/mean
// //www.sitepoint.com/community/t/calculating-the-average-mean/7302/3
//
/**
* The "mean" is the "average" you're used to, where you add up all the numbers
* and then divide by the number of numbers.
*
* For example, the "mean" of [3, 5, 4, 4, 1, 1, 2, 3] is 2.875.
*
* @param {Array} numbers An array of numbers.
* @return {Number} The calculated average [or mean] value from the specified
* numbers.
*/
function mean[numbers] {
var total = 0, i;
for [i = 0; i < numbers.length; i += 1] {
total += numbers[i];
}
return total / numbers.length;
}
//
// alternative mean/average method [from //www.30secondsofcode.org/snippet/average]:
const mean = [...numbers] => numbers.reduce[[acc, val] => acc + val, 0] / numbers.length;
//
// usage:
mean[...[1,2,3]]; // 2
mean[...[0,1,2,3,4,5,6,7,8,9,10]]; // 5
mean[...[1,2,3]]; // 2
/**
* The "median" is the "middle" value in the list of numbers.
*
* @param {Array} numbers An array of numbers.
* @return {Number} The calculated median value from the specified numbers.
*/
function median[numbers] {
// median of [3, 5, 4, 4, 1, 1, 2, 3] = 3
var median = 0, numsLen = numbers.length;
numbers.sort[];
if [
numsLen % 2 === 0 // is even
] {
// average of two middle numbers
median = [numbers[numsLen / 2 - 1] + numbers[numsLen / 2]] / 2;
} else { // is odd
// middle number only
median = numbers[[numsLen - 1] / 2];
}
return median;
}
/**
* The "mode" is the number that is repeated most often.
*
* For example, the "mode" of [3, 5, 4, 4, 1, 1, 2, 3] is [1, 3, 4].
*
* @param {Array} numbers An array of numbers.
* @return {Array} The mode of the specified numbers.
*/
function mode[numbers] {
// as result can be bimodal or multi-modal,
// the returned result is provided as an array
// mode of [3, 5, 4, 4, 1, 1, 2, 3] = [1, 3, 4]
var modes = [], count = [], i, number, maxIndex = 0;
for [i = 0; i < numbers.length; i += 1] {
number = numbers[i];
count[number] = [count[number] || 0] + 1;
if [count[number] > maxIndex] {
maxIndex = count[number];
}
}
for [i in count]
if [count.hasOwnProperty[i]] {
if [count[i] === maxIndex] {
modes.push[Number[i]];
}
}
return modes;
}
/**
* The "range" of a list a numbers is the difference between the largest and
* smallest values.
*
* For example, the "range" of [3, 5, 4, 4, 1, 1, 2, 3] is [1, 5].
*
* @param {Array} numbers An array of numbers.
* @return {Array} The range of the specified numbers.
*/
function range[numbers] {
numbers.sort[];
return [numbers[0], numbers[numbers.length - 1]];
}
Chế độ trong JavaScript là gì?
Tính toán chế độ của một tập hợp số hoặc danh sách có giá trị [số hoặc ký tự] . Nếu có nhiều hơn một chế độ, nó sẽ trả về một danh sách các giá trị đó.
Làm cách nào để sử dụng chế độ trong JavaScript?
Chế độ nghiêm ngặt được khai báo bằng cách thêm "sử dụng nghiêm ngặt";
Làm cách nào để tìm trung bình và chế độ trung bình trong js?
Nó có thể được tính bằng cách cộng tất cả các số và chia cho kích thước mảng . Hãy tính giá trị trung bình cho mảng sau. [2, 4, 5, 7, 1, 8, 1]. Cộng tất cả các số. 2 + 4 + 5 + 7 + 1 + 8 + 1 = 28. Chia tổng cho kích thước. 28/7 = 4.