-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy paththe-highest-profit-wins.js
84 lines (73 loc) · 1.95 KB
/
the-highest-profit-wins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function minMax(arr){
// sort the array
arr.sort((a, b) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
// return the first and last values from the array
const min = arr[0];
const max = arr[arr.length - 1];
return [min, max];
}
function minMax(arr){
// sort the array
arr.sort((a, b) => a - b);
// return the first and last values from the array
const min = arr[0];
const max = arr[arr.length - 1];
return [min, max];
}
function minMax(arr) {
if (arr.length === 1) return [arr[0], arr[0]];
let min;
let max;
// min variable set to the lowest of the first 2 values in the array
// max variable set to the highest of the first 2 values in the array
if (arr[0] < arr[1]) {
min = arr[0];
max = arr[1];
} else {
max = arr[0];
min = arr[1];
}
// iterate up from the 3rd value to the end of the array
for (let i = 2; i < arr.length; i++) {
const currentValue = arr[i];
// if the current value is less than the current min
if (currentValue < min) {
// set the min to be the current value
min = currentValue;
}
if (currentValue > max) {
// else if the current value is greater than the current max
// set the max to be the current value
max = currentValue;
}
}
// return max and min
return [min, max];
}
function minMax(arr) {
const min = Math.min(...arr);
// const min = Math.min.apply(null, arr);
const max = Math.max(...arr);
// const max = Math.max.apply(null, arr);
return [min, max];
}
function minMax(arr) {
if (arr.length === 1) return [arr[0], arr[0]];
return arr.reduce(([min, max], value) => {
if (value < min) {
min = value;
}
if (value > max) {
max = value;
}
return [min, max];
}, [Infinity, -Infinity]);
}
console.log(minMax([1,2,3,4,5]), [1,5]);
console.log(minMax([2334454,5]), [5, 2334454]);
console.log(minMax([1]), [1, 1])
console.log(minMax([100, 1]), [1, 100])