forked from ChristianNally/web-2023-Feb-06-west-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathq1.js
More file actions
91 lines (77 loc) · 1.84 KB
/
q1.js
File metadata and controls
91 lines (77 loc) · 1.84 KB
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
85
86
87
88
89
90
91
/* Question 1
*
* Implement the functions defined below
*
*/
/* ===========================================================================
* MIN - the lowest value in a list
*
* For example:
*
* min([6,2,3,4,9,6,1,0,5])
*
* Returns:
*
* 0
*/
const min = function(arr) {
// return Math.min(...arr);
// create a space in memory to hold lowest value seen so far
let lowestValue = arr[0];
// look at each value in the arr
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
// is this the lowest value we've seen so far?
if (value < lowestValue) {
// if yes, replace the lowest value with the current value
lowestValue = value;
}
}
// return lowest value seen
return lowestValue;
};
/* ===========================================================================
* MAX - the highest value in a list
*
* For example:
*
* max([6,2,3,4,9,6,1,0,5])
*
* Returns:
*
* 9
*/
const max = function(arr) {
// create a variable to hold highest seen so far
let highestValue = arr[0];
// look at each element inside the array
for (let i = 0; i < arr.length; i++) {
const value = arr[i];
// is this higher than the highest value seen so far?
if (value > highestValue) {
// if yes, replace highest value with current value
highestValue = value;
}
}
// return highest seen
return highestValue;
};
/* ===========================================================================
* RANGE - the difference between the highest and lowest values in a list
*
* For example:
*
* range([6,2,3,4,9,6,1,0,5])
*
* Returns:
*
* 9
*/
const range = function(arr) {
const highestValue = max(arr);
const lowestValue = min(arr);
const difference = highestValue - lowestValue;
return difference;
};
// Don't change below:
module.exports = { min, max, range };