From 2ff2fcdc51c6bad5b0bd61f645226e7838c26579 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 26 Jan 2021 01:54:23 +0100 Subject: Node.js solution for week 097, part 1 --- challenge-097/abigail/node/ch-1.js | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 challenge-097/abigail/node/ch-1.js 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 (""))) +; -- cgit