diff options
| author | Abigail <abigail@abigail.be> | 2021-03-04 20:22:21 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-04 20:22:21 +0100 |
| commit | 53f8a2c0c28815d2ae4eb67f0fdd378462c74147 (patch) | |
| tree | fa4ea84234b8ed161e12a9fc7293baa6c5cf91fd | |
| parent | fbab37eacec781a9f3965545228ccf09abe71071 (diff) | |
| download | perlweeklychallenge-club-53f8a2c0c28815d2ae4eb67f0fdd378462c74147.tar.gz perlweeklychallenge-club-53f8a2c0c28815d2ae4eb67f0fdd378462c74147.tar.bz2 perlweeklychallenge-club-53f8a2c0c28815d2ae4eb67f0fdd378462c74147.zip | |
Node.js solution for week 4, part 2
| -rw-r--r-- | challenge-004/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-004/abigail/node/ch-2.js | 46 | ||||
| -rw-r--r-- | challenge-004/abigail/t/ctest.ini | 3 |
3 files changed, 50 insertions, 0 deletions
diff --git a/challenge-004/abigail/README.md b/challenge-004/abigail/README.md index b26977cfc3..382ede17cd 100644 --- a/challenge-004/abigail/README.md +++ b/challenge-004/abigail/README.md @@ -49,4 +49,5 @@ The sets of letters are read from standard input. * [Bash](bash/ch-2.sh) * [C](c/ch-2.c) * [Lua](lua/ch-2.lua) +* [Node.js](node/ch-2.js) * [Perl](perl/ch-2.pl) diff --git a/challenge-004/abigail/node/ch-2.js b/challenge-004/abigail/node/ch-2.js new file mode 100644 index 0000000000..a239d89949 --- /dev/null +++ b/challenge-004/abigail/node/ch-2.js @@ -0,0 +1,46 @@ +#!/usr/local/bin/node + +// +// See ../README.md +// + +// +// Run as: node ch-2.js -f filename < input-file +// + +const fs = require ('fs') +const readline = require ('readline') +const yargs = require ('yargs') + +// +// Parse options using the yargs module +// +const argv = yargs . option ('file', { + alias: 'f', + type: 'string', + }) + . argv; + +let filename = argv . file; + +// +// Extract the words from the file 'filename' which can be +// made with the letters from 'letters'. +// +function find_words (filename, letters) { + letters = letters . toLowerCase () + . split ("") + readline . createInterface ({input: fs . createReadStream (filename)}) + . on ('line', word => { + let copy = word . toLowerCase () + letters . forEach (l => { + copy = copy . replace (l, "") + }) + if (copy == "") { + console . log (word) + } + }) +} + +readline . createInterface ({input: process . stdin}) + . on ('line', letters => find_words (filename, letters)) diff --git a/challenge-004/abigail/t/ctest.ini b/challenge-004/abigail/t/ctest.ini index 6675a345b1..161f1c35f0 100644 --- a/challenge-004/abigail/t/ctest.ini +++ b/challenge-004/abigail/t/ctest.ini @@ -6,6 +6,9 @@ [challenges/1]
extra_tests = Check_Program_Size
+[2-1/node]
+line_at_a_time = 1
+
[2-1]
args = -f t/words.txt
|
