aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-05-24 12:45:26 -0400
committerchirvasitua <stuart-little@users.noreply.github.com>2021-05-24 20:25:17 -0400
commit3379446eed6b866920996d2afb545a6932fd7534 (patch)
tree546139c1d01834a2862684579372919210cce621
parenta2dcbbbcc3a246b0136e40aee20170a021f0b573 (diff)
downloadperlweeklychallenge-club-3379446eed6b866920996d2afb545a6932fd7534.tar.gz
perlweeklychallenge-club-3379446eed6b866920996d2afb545a6932fd7534.tar.bz2
perlweeklychallenge-club-3379446eed6b866920996d2afb545a6932fd7534.zip
1st commit on 114_node
-rwxr-xr-xchallenge-114/stuart-little/node/ch-1.js22
-rwxr-xr-xchallenge-114/stuart-little/node/ch-2.js9
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-114/stuart-little/node/ch-1.js b/challenge-114/stuart-little/node/ch-1.js
new file mode 100755
index 0000000000..021d79e561
--- /dev/null
+++ b/challenge-114/stuart-little/node/ch-1.js
@@ -0,0 +1,22 @@
+#!/usr/bin/env node
+
+// run <script> <number>
+
+const nrDig = process.argv[2].length;
+
+if (process.argv[2] === '9'.repeat(nrDig)) {
+ console.log(parseInt(process.argv[2])+2);
+ process.exit();
+}
+
+function doubleUp(nrDig, initHalf) {
+ return initHalf + ((nrDig % 2) ? (initHalf.substring(0,initHalf.length-1).split('').reverse().join('')) : (initHalf.split('').reverse().join('')))
+}
+
+const initHalf = process.argv[2].substring(0,Math.floor((nrDig+1)/2));
+
+console.log(
+ (parseInt(doubleUp(nrDig,initHalf)) > parseInt(process.argv[2])) ?
+ (doubleUp(nrDig,initHalf)) :
+ (doubleUp(nrDig,(parseInt(initHalf)+1).toString()))
+)
diff --git a/challenge-114/stuart-little/node/ch-2.js b/challenge-114/stuart-little/node/ch-2.js
new file mode 100755
index 0000000000..dda39b3868
--- /dev/null
+++ b/challenge-114/stuart-little/node/ch-2.js
@@ -0,0 +1,9 @@
+#!/usr/bin/env node
+
+// run <script> <number>
+
+const binNr = '0'+parseInt(process.argv[2]).toString(2);
+console.log(`Initial number in base two: ${binNr}`);
+const nxt = (process.argv[2] % 2) ? (binNr.replace(/01(1*)$/, '10$1')) : (binNr.replace(/01(1*)(0*)$/, '10$2$1'));
+console.log(`Next number in base two: ${nxt}`);
+console.log(`Next number in base ten: ${parseInt(nxt,2)}`)