aboutsummaryrefslogtreecommitdiff
path: root/challenge-097/abigail/node/ch-1.js
blob: 18c36b116240f14a90b02296d7b2e91d9d1aa776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/local/bin/node

//
// See ../README.md
//

//
// Run as: node ch-1.js -s TIMES < input-file
//

const NR_OF_LETTERS = 26
const ORD_A         = "A" . charCodeAt (0)

//
// 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 < ORD_A) {
           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  ("")))
;