-2

I am learning javascript & Node.js. For some reason this IF statement is not working as I would've expected...
I cannot figure out how to solve this...

My code:

process.stdin.setEncoding('utf8');                      //Set UTF charcode

process.stdin.on('readable', () => {                    //Event fires when there's input
    var readConsole = process.stdin.read();             //Receive the input from console

    if(readConsole != null) {
        readConsole.trim().replace(/\r?\n|\r/g, " ");   //Trim input and remove line breaks

        process.stdout.write('Input: ' + readConsole);  //Output the input

        if(readConsole == "quit") {
            process.exit();
        }
    }
});

But for some reason, whenever I type "quit" in the console, it does not respond.

Here is an image of the problem:

enter image description here

zx485
  • 28,498
  • 28
  • 50
  • 59
Kerwin Sneijders
  • 750
  • 13
  • 33

1 Answers1

4

Both trim() and replace() return a new string, but you are not assigning that value to any variable. You probably want to remove extra characters, not replace them with spaces (thanks @fvgs). Try:

readConsole = readConsole.trim().replace(/\r?\n|\r/g, "");
Keammoort
  • 3,075
  • 15
  • 20
  • The replacement string should probably be `""`, otherwise the condition in the if statement will need to be `readConsole == "quit "`. – fvgs Jan 15 '17 at 01:22
  • Owh, I did that at first. but removing the spaces worked this way too.... Anyway, thanks man 10/10!! – Kerwin Sneijders Jan 15 '17 at 09:05