diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-02 00:18:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-02 00:18:27 +0100 |
| commit | fa7af9fdedddd80815045a1b0e23f31acff4740a (patch) | |
| tree | c58db0f2ae8e756a3f8b08337688c539fef0ebfa | |
| parent | 4bcd01c15223d9f1cdd85399a62fb3f79e3c7c33 (diff) | |
| parent | 70a60ae93a5ac9cef7380bc42a1d8581e3ef790b (diff) | |
| download | perlweeklychallenge-club-fa7af9fdedddd80815045a1b0e23f31acff4740a.tar.gz perlweeklychallenge-club-fa7af9fdedddd80815045a1b0e23f31acff4740a.tar.bz2 perlweeklychallenge-club-fa7af9fdedddd80815045a1b0e23f31acff4740a.zip | |
Merge pull request #2427 from jacoby/master
JS and Blog
| -rw-r--r-- | challenge-080/dave-jacoby/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-080/dave-jacoby/node/ch-1.js | 25 | ||||
| -rwxr-xr-x | challenge-080/dave-jacoby/node/ch-2.js | 22 |
3 files changed, 48 insertions, 0 deletions
diff --git a/challenge-080/dave-jacoby/blog.txt b/challenge-080/dave-jacoby/blog.txt new file mode 100644 index 0000000000..95ae4e56fd --- /dev/null +++ b/challenge-080/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2020/09/30/challenge-80.html diff --git a/challenge-080/dave-jacoby/node/ch-1.js b/challenge-080/dave-jacoby/node/ch-1.js new file mode 100755 index 0000000000..6e23ec8521 --- /dev/null +++ b/challenge-080/dave-jacoby/node/ch-1.js @@ -0,0 +1,25 @@ +"use strict" + +console.log( spnm( [5, 2, -2, 0 ] )); +console.log( spnm( [1, 8, -1 ] )); +console.log( spnm( [2, 0, -1 ] )); +console.log( spnm( Array(12).fill().map((n, i) => 1 + i) )); + +function spnm ( array ) { + let list = array.filter( i => i > 0 ); + let max = 1 + Math.max(...list); + let range = Array(max).fill().map((n, i) => 1 + i) ; + let hash = {}; + + for ( let i in list ) { + let n = list[i]; + hash[n]=1; + } + + for ( let i in range ) { + let n = range[i]; + if ( !hash[n] ) { return n } + } + return -1 +} + diff --git a/challenge-080/dave-jacoby/node/ch-2.js b/challenge-080/dave-jacoby/node/ch-2.js new file mode 100755 index 0000000000..a47fe0eb2b --- /dev/null +++ b/challenge-080/dave-jacoby/node/ch-2.js @@ -0,0 +1,22 @@ +"use strict" + +console.log( candy_count( [1, 2, 2] ) ); +console.log( candy_count( [1, 4, 3, 2] ) ); + + +function candy_count( candidates ) { + let total = 0; + console.log( candidates ); + for ( let i in candidates ) { + i = parseInt(i); // string by default + let v = candidates[i]; + let prev = candidates[i-1] || 0; + let next = candidates[i+1] || 0; + total ++; + if ( v > prev && prev != 0 ) { total ++ } + if ( v > next && next != 0 ) { total ++ } + } + return total; +} + + |
