0

In my project, I need now to take an input from the keyboard. The user must be able to enter several words and when he presses CTRL+D you exit the program and the result is displayed. For example we can enter on the terminal :

bob
alicia
cookie
shirley
david

We have the following code :

#!/usr/bin/env node

let chunk = "";

process.stdin.on("data", data => {
    chunk += data.toString();
});

process.stdin.on("end", () => {
    chunk.replace(/^\s*[\r\n]/gm,"").split(/\s+/).forEach(function (s) {
        process.stdout.write(
        s === 'bob'
        ? 'boy \n'
        : s === 'alicia'
           ? 'girl\n'
           : s === 'cookie'
               ? 'dog \n'
               : 'unknown \n');
    });
});

And when we press CTRL+D we need to obtain this result :

boy
girl
dog
unknown
unknown

Can you help me please to know, how can I code in order to take the keyboard like an input?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    The `replace()` function is removing all the spaces, so how can you split the result on spaces? – Barmar Oct 09 '18 at 15:22
  • Where do you set the value of `chunk` before calling `chunk.replace()`? – Barmar Oct 09 '18 at 15:22
  • What isn't working about the code you've got? – Heretic Monkey Oct 09 '18 at 15:26
  • @HereticMonkey @barmar the right goal it's to write `bob alicia shirley` in the terminal and when the user press CTRL+D we can display the answer like `boy girl unknown`. But here I take a file like an input because I don't know how can we put something we wrote on the keyboard like an entry. –  Oct 09 '18 at 15:28
  • Sounds like [Reading value from console, interactively](https://stackoverflow.com/q/8128578)... – Heretic Monkey Oct 09 '18 at 15:31
  • Think you I found the solution @HereticMonkey –  Oct 09 '18 at 15:54

2 Answers2

0

Here is an article that explains the basics. I made an example for you down below, you can probably figure out the rest by yourself.

const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

let input = [];
let chunk = '';

process.stdin.on('keypress', (str, key) => {
    if (key.ctrl && key.name === 'd') {
        //Handle exit code here
        process.exit();
    }
    if (key.name === 'return') {
        input.push(chunk.replace('\r', ''));
        chunk = '';
        process.stdout.write('\n');
    }
    chunk+=str;
    process.stdout.write(str);
});
-1

One way of doing it would to loop input until a certain input is taken. Pseudo code example:

While (x≠q){
    Take input
 }

EDIT: Another way, is to not use the return key to use for spacing, instead take the items in all in one input line with a separating comma or space.

var str = "123, 124, 234,252";
var arr = str.split(",").map(val => Number(val) + 1);
console.log(arr); 

I found the above from this question: How to split and modify a string in NodeJS?.
Then you can iterate over the array to find out if it's a dog or a girl!

Evsus
  • 11
  • 4
  • I saw this but I don't really know how can I code this correctly and mix with my code –  Oct 09 '18 at 15:29
  • 2
    I have a much stronger background in Perl than JS, so my first thought would be to check the input against a regular expression – Evsus Oct 09 '18 at 15:42