diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2020-07-30 18:18:30 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2020-07-30 18:18:30 -0400 |
| commit | 4dd6772677eb3c1cee6cd7f70997d3e2ecdcda6d (patch) | |
| tree | a7e80da00fb3f029f90a70056ed14a0b6a342efb /challenge-071/dave-jacoby | |
| parent | 76ab89fd6cb452d0ecf26bd42fa4ae88177817ec (diff) | |
| download | perlweeklychallenge-club-4dd6772677eb3c1cee6cd7f70997d3e2ecdcda6d.tar.gz perlweeklychallenge-club-4dd6772677eb3c1cee6cd7f70997d3e2ecdcda6d.tar.bz2 perlweeklychallenge-club-4dd6772677eb3c1cee6cd7f70997d3e2ecdcda6d.zip | |
Node!
Diffstat (limited to 'challenge-071/dave-jacoby')
| -rwxr-xr-x | challenge-071/dave-jacoby/node/ch-1.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-071/dave-jacoby/node/ch-1.js b/challenge-071/dave-jacoby/node/ch-1.js new file mode 100755 index 0000000000..d162c39917 --- /dev/null +++ b/challenge-071/dave-jacoby/node/ch-1.js @@ -0,0 +1,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; +} |
