-
Notifications
You must be signed in to change notification settings - Fork 320
/
ch-1.js
executable file
·66 lines (55 loc) · 1.21 KB
/
ch-1.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
"use strict;";
// get N
// argv[0] = node
// argv[1] = this program
let n = process.argv[2];
n = parseInt(n);
process.on("exit", function (code) {
if (code != 0) {
return console.log(`About to exit with code ${code}`);
}
});
if (!Number.isInteger(n)) {
n = 7;
}
// control number range;
if (n < 1 || n > 50) {
process.exit(22);
}
// do the work
let values = create_array(n);
let peaks = find_peaks(values);
// remove the beginning and ending 0
values.pop();
values.shift();
console.log('VALUES');
console.dir(values);
console.log('');
console.log('PEAKS');
console.dir(peaks);
// find peak values in the array
function find_peaks(values) {
let output = [];
for (let i = 0; i < values.length; i++) {
if (i > 0 && i < values.length - 1) {
if (values[i] > values[i - 1] && values[i] > values[i + 1]) {
output.push(values[i]);
}
}
}
return output;
}
// create array
function create_array(max) {
let values = [];
while (values.length < max) {
let value = 1 + Math.floor(50 * Math.random());
if (!values.includes(value)) {
values.push(value);
}
}
// adding zero to beginning to end removes odd cases
values.unshift(0);
values.push(0);
return values;
}