You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB
TypeScript

// export const min = (list: number[]): number => {
// const sortList = list.sort();
// let L = 0;
// let R = list.length - 1;
// let res = list[L];
// while (L <= R) {
// let currentMin = sortList[L] < sortList[R] ? sortList[L] : sortList[R];
// res = currentMin < res ? currentMin : res;
// L++;
// R--;
// }
// return res;
// };
// export const max = (list: number[]): number => {
// const sortList = list.sort();
// let L = 0;
// let R = list.length - 1;
// let res = list[L];
// while (L <= R) {
// let currentMax = sortList[L] > sortList[R] ? sortList[L] : sortList[R];
// res = currentMax > res ? currentMax : res;
// L++;
// R--;
// }
// return res;
// };
// const min = (list) => Math.min(...list);
// const max = (list) => Math.max(...list);
export const min = (list: number[]): number => {
return list.sort((leftvalue, rightvalue): number => {
return leftvalue - rightvalue;
})[0];
};
export const max = (list: number[]): number => {
return list.sort((leftvalue, rightvalue): number => {
return leftvalue - rightvalue;
})[list.length - 1];
};
console.log(min([-52, 56, 30, 29, -54, 0, -110]), -110);
console.log(min([42, 54, 65, 87, 0]), 0);
console.log(max([4, 6, 2, 1, 9, 63, -134, 566]), 566);
console.log(max([5]), 5);