forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMaxRecursive.js
40 lines (34 loc) · 926 Bytes
/
MinMaxRecursive.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
const MAX_LENGTH = 10;
/**
* @param {number[]} vector
* @param {number} min
* @param {number} max
* @param {number} index
*/
function MinMaxRecursive(vector, min, max, index){
if (vector[index] < min) min = vector[index];
if (vector[index] > max) max = vector[index];
if (index < vector.length - 1) {
const result = MinMaxRecursive(vector, min, max, index + 1);
min = result.min;
max = result.max;
}
return {
min,
max
}
}
function main(){
let vector = [];
let values = "[";
for (let index = 0; index < MAX_LENGTH; index++) {
let value = Math.floor(Math.random() * 99); // 0 - 99
vector.push(value);
values += `${value}, `;
}
values += "]"
console.log(values);
const result = MinMaxRecursive(vector, vector[0], vector[0], 1);
console.log(`Min: ${result.min}\nMax: ${result.max}`);
}
main();