-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[js] 第29天 写一个获取数组的最大值、最小值的方法 #104
Comments
|
Math.max.apply(Array,[25,62,91,78,34,62]) // 91
Math.min.apply(Array,[27,64,90,78,34,62]) // 27 |
|
// 利用 reduce 冒泡排序
const getMax = arr => arr.reduce((acc, val) => acc = acc < val ? val : acc)
getMax([25, 62, 91, 78, 34, 62]) // 91 |
Array.prototype.max = function() {
return Math.max.apply(null, this)
} es6 Math.max(...array) |
let arr = [1,2,3,4]
Math.max(...arr)
Math.min(...arr) |
因为我是跳着做的,这题可以说是 Day54 的前置题目了。 const getMinAndMax = arr => ({
// 数组展开在 ES5 中可以用 apply 代替
// ES5: Math.min.apply(null, arr)
min: Math.min(...arr),
max: Math.max(...arr)
});
console.log(getMinAndMax([1, 2, 3, 4, 5, 6, 7, 8, 9]));
console.log(getMinAndMax([12, 223, 13, 4, 25, 36, 7, 18, 90])); |
|
const getMaxFromArr = arr => Math.max(...arr);
const getMinFromArr = arr => Math.min(...arr); |
Math.max(...array) |
ES6之前用apply代替,作用域无所谓 |
这不是冒泡排序吧? |
|
封装了一个方法
|
var arr = [27, 64, 90, 78, 34, 62];
var newArr = arr.sort((a, b) => a - b);
var max = newArr[newArr.length - 1];
var min = newArr[0];
console.log(max);
console.log(min); |
function max() {
return Math.max.apply(null, arguments);
}
function min() {
return Math.min.apply(null, arguments);
} |
|
let arr = [1, 2, 3, 4, 5, 6] Math.max.apply(null, arr) Math.max(...arr) arr.reduce((a, b) => Math.max(a, b)) let temp = arr.sort((a, b) => a - b) |
1. 排序法首先我们对数组进行排序,可以按照升序排序,也可以按照降序排序。排序之后的数组中的第一个和最后一个元素就是我们需要的最大值或最小值。 排序可以使用 // 方法一:排序法
arr.sort(function (a, b) {
// 升序排序
return a - b;
})
maxValue = arr[arr.length - 1];
minValue = arr[0];
console.log('最大值为:', maxValue); // 91
console.log('最小值为:', minValue); // 25 2. 假设法可以假设数组中的第一个值是最大值,然后拿这个最大值和数组中的其他元素逐一比较,如果后面的某一个值比假设的值还大,说明假设错了,我们把假设的值进行替换,将这个值赋假设为新的最大值。循环结束后,拿到的结果就是我们想要的。最小值反之。 // 假设第一个数组元素就是数组中的最大值
maxValue = arr[0];
for (let i = 0; i < arr.length; i++) {
let cur = arr[i];
// 如果当前元素的值比假设的最大值大,就设置当前值为最大值,继续和后面元素比较
maxValue = cur > maxValue ? cur : maxValue;
}
console.log('最大值为:', maxValue);
// 假设第一个数组元素就是数组中的最小值
minValue = arr[0];
for (let i = 0; i < arr.length; i++) {
let cur = arr[i];
// 如果当前元素的值比假设的最小值小,就设置当前值为最小值,继续和后面元素比较
minValue = cur < minValue ? cur : minValue;
}
console.log('最小值为:', minValue); 3. 使用
|
|
Math.max.apply(Array,[25,62,91,78,34,62]) // 91 |
Math.max 和 Math.min 虽然能实现, 但是如果这道题目希望的是同时获取最大值和最小值,应该是没有捷径的,以数组的第一个元素作为参照值遍历一遍的时间复杂度是n,另外如果用排序实现,显然是不合理的。 |
看了评论 自己居然没想到Math.max
|
console.log(Math.max(...array)); |
// 获取数组最大值 最小值 |
let arr = [2, 4, 6, 8, 1, 23, 12, 2, 44, 6, 865, 4, 2, 5, 6, 7, 89, 87]
let min, max
for (let index = 0; index < arr.length; index++) {
if (index === 0) {
min = max = arr[index];
} else {
if (arr[index] > max) {
max = arr[index];
}
if (arr[index] < min) {
min = arr[index]
}
}
}
console.log(min,max); |
function getMaxMin(arr){
let newArr = arr.sort((a, b) => {
return a - b;
})
return {
Max: newArr[newArr.length - 1],
Min: newArr[0];
}
} |
您好!我看到邮件后会立即联系您!
|
在JavaScript中,可以使用以下方法来获取数组的最大值和最小值: 1. 使用Math对象的max()和min()方法:
const array = [1, 5, 3, 2, 4];
const max = Math.max(...array);
const min = Math.min(...array);
console.log(max); // 输出最大值
console.log(min); // 输出最小值
2. 使用reduce()方法:
const array = [1, 5, 3, 2, 4];
const max = array.reduce((a, b) => Math.max(a, b));
const min = array.reduce((a, b) => Math.min(a, b));
console.log(max); // 输出最大值
console.log(min); // 输出最小值
3. 使用循环遍历数组:
const array = [1, 5, 3, 2, 4];
let max = array[0];
let min = array[0];
for (let i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
} console.log(max); // 输出最大值 |
在JavaScript中,可以使用以下方法来获取数组的最大值和最小值: 1. 使用Math对象的max()和min()方法:
const array = [1, 5, 3, 2, 4];
const max = Math.max(...array);
const min = Math.min(...array);
console.log(max); // 输出最大值
console.log(min); // 输出最小值
2. 使用reduce()方法:
const array = [1, 5, 3, 2, 4];
const max = array.reduce((a, b) => Math.max(a, b));
const min = array.reduce((a, b) => Math.min(a, b));
console.log(max); // 输出最大值
console.log(min); // 输出最小值
3. 使用循环遍历数组:
const array = [1, 5, 3, 2, 4];
let max = array[0];
let min = array[0];
for (let i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
}
console.log(max); // 输出最大值
console.log(min); // 输出最小值 以上三种方法都可以获取数组的最大值和最小值。根据具体的情况和需求,选择适合的方法来处理数组。 |
第29天 写一个获取数组的最大值、最小值的方法
The text was updated successfully, but these errors were encountered: