aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-26 01:54:23 +0100
committerAbigail <abigail@abigail.be>2021-01-26 12:07:41 +0100
commit2ff2fcdc51c6bad5b0bd61f645226e7838c26579 (patch)
treee3b22ba01724a0db4f19e23fbc2e9766eb3a4f8c
parent3a68dc14c70c709ea8097e98e38931ef93e643af (diff)
downloadperlweeklychallenge-club-2ff2fcdc51c6bad5b0bd61f645226e7838c26579.tar.gz
perlweeklychallenge-club-2ff2fcdc51c6bad5b0bd61f645226e7838c26579.tar.bz2
perlweeklychallenge-club-2ff2fcdc51c6bad5b0bd61f645226e7838c26579.zip
Node.js solution for week 097, part 1
-rw-r--r--challenge-097/abigail/node/ch-1.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-097/abigail/node/ch-1.js b/challenge-097/abigail/node/ch-1.js
new file mode 100644
index 0000000000..0cba6f1a23
--- /dev/null
+++ b/challenge-097/abigail/node/ch-1.js
@@ -0,0 +1,50 @@
+#!/usr/local/bin/node
+
+//
+// See ../README.md
+//
+
+//
+// Run as: node ch-1.js -s TIMES < input-file
+//
+
+const NR_OF_LETTERS = 26
+
+//
+// Parse input
+//
+const argv = require ('yargs')
+. option ('s', {
+ type: 'number',
+ })
+. demandOption ('s')
+. argv;
+
+const shift = argv . s
+
+//
+// Shift capital letters, by a given amount.
+//
+function shift_char (char, shift) {
+ if (char . match (/[A-Z]/)) {
+ let n = char . charCodeAt (0) - (shift % NR_OF_LETTERS)
+ if (n < 65) {
+ n = n + NR_OF_LETTERS
+ }
+ return String . fromCharCode (n)
+ }
+ else {
+ return char
+ }
+}
+
+
+//
+// Iterate over the input, and shift characters
+//
+require ('readline')
+. createInterface ({input: process . stdin})
+. on ('line', _ => console . log (_ . split ("")
+ . map (_ => shift_char (_, shift))
+ . join ("")))
+;