diff options
| author | Abigail <abigail@abigail.be> | 2021-03-03 01:33:25 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-03 01:33:25 +0100 |
| commit | 1ed243eb389da924b7a89910bec23c4faa720170 (patch) | |
| tree | 694dd52a6e57b5ecdb4ee21e0e803a06283460b3 /challenge-102/abigail/node | |
| parent | 8f07b40ae63f584a00ed8a3d60df728aaa534f46 (diff) | |
| download | perlweeklychallenge-club-1ed243eb389da924b7a89910bec23c4faa720170.tar.gz perlweeklychallenge-club-1ed243eb389da924b7a89910bec23c4faa720170.tar.bz2 perlweeklychallenge-club-1ed243eb389da924b7a89910bec23c4faa720170.zip | |
Node.js solution for week 102, part 2
Diffstat (limited to 'challenge-102/abigail/node')
| -rw-r--r-- | challenge-102/abigail/node/ch-2.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-102/abigail/node/ch-2.js b/challenge-102/abigail/node/ch-2.js new file mode 100644 index 0000000000..92219dec3f --- /dev/null +++ b/challenge-102/abigail/node/ch-2.js @@ -0,0 +1,36 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js < input-file +// + +require ('readline') +. createInterface ({input: process . stdin}) +. on ('line', _ => make_hash (+ _)) +; + + +// +// Working from the end of the required string backwards, we alternate +// placing a hash, and placing a number. We place them in an array @out, +// and at the end, print out said array in reverse order. +// +function make_hash (index) { + let out = [] + let hash = false + while (index > 0) { + if (hash = !hash) { + out . push ("#") + index -- + } + else { + out . push (index + 1) + index -= (index + 1) . toString () . length + } + } + console . log (out . reverse () . join ("")) +} |
