diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-07-30 23:29:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-30 23:29:53 +0100 |
| commit | ab4664c5f733997e92e3357a515d003d6a5fd485 (patch) | |
| tree | a7e80da00fb3f029f90a70056ed14a0b6a342efb | |
| parent | 19461982a4a33d36f6bd37959fa1f07e61d544d8 (diff) | |
| parent | 4dd6772677eb3c1cee6cd7f70997d3e2ecdcda6d (diff) | |
| download | perlweeklychallenge-club-ab4664c5f733997e92e3357a515d003d6a5fd485.tar.gz perlweeklychallenge-club-ab4664c5f733997e92e3357a515d003d6a5fd485.tar.bz2 perlweeklychallenge-club-ab4664c5f733997e92e3357a515d003d6a5fd485.zip | |
Merge pull request #2005 from jacoby/master
Node!
| -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; +} |
