javascript number guessing game is outputting random information

Viewed 65

I'm trying to create a game that randomly picks a number between 1 and 10. The problem is that for some reason, when I compile the code, the program outputs all this extra data and I can't seem to figure out why it's doing it. Can I please have some help? Below is all my code.

I'm using Replit

var readline = require('readline');

var randomNumber = Math.round(Math.random() * 10);
var lives = 5;

var terminal = readline.createInterface(
{
  input : process.stdin,
  output : process.stdout
});

terminal.setPrompt('Guess the number! (0-10): ');
terminal.prompt();
terminal.on('line', function(answer)
{
  var answerNum = parseInt(answer);

  if (answerNum > randomNumber)
  {
    console.log('Too high!');
    console.log('You have '+lives+' lives left');
  }

  else if (answerNum < randomNumber)
  {
    console.log('Too low!');
    console.log('You have '+lives+' lives left');
  }

  else if (answerNum === randomNumber)
  {
    console.log('W I N N E R ! ! !');
    console.log('You lost only '+ (6-lives) + ' lives');
    process.exit(0);
  }

  else
  {
    console.log("That wasn't a number I recognise");
    console.log('You have '+lives+' lives');
  }

  lives--;
  if (lives == 0)
  {
    console.log('G A M E  O V E R ! ! !');
    process.exit(0);
  }

  terminal.prompt();
});

terminal.on('close', function()
{
  console.log('C H I C K E N :P')
  process.exit(1);
});

this is what it's outputting

Guess the number! (0-10): Interface {
  _sawReturnAt: 0,
  isCompletionEnabled: true,
  _sawKeyPress: false,
  _previousKey: null,
  escapeCodeTimeout: 500,
  _events: [Object: null prototype] {
    close: [ [Function], [Function] ],
    '': [Function]
  },
  _eventsCount: 2,
  _maxListeners: undefined,
  output: WriteStream {
    connecting: false,
    _hadError: false,
    _parent: null,
    _host: null,
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 16384,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: null,
      pipesCount: 0,
      flowing: null,
      ended: false,
      endEmitted: false,
      reading: false,
      sync: true,
      needReadable: false,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: false,
      emitClose: false,
      autoDestroy: false,
      destroyed: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: false,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: null
    },
    readable: false,
    _events: [Object: null prototype] {
      end: [Function: onReadableStreamEnd],
      resize: [Function: onresize]
    },
    _eventsCount: 2,
    _maxListeners: undefined,
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 16384,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: false,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: false,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      afterWriteTickInfo: [Object],
      bufferedRequest: null,
      lastBufferedRequest: null,
      pendingcb: 4,
      prefinished: false,
      errorEmitted: false,
      emitClose: false,
      autoDestroy: false,
      bufferedRequestCount: 0,
      corkedRequestsFree: [Object]
    },
    writable: true,
    allowHalfOpen: false,
    _sockname: null,
    _pendingData: null,
    _pendingEncoding: '',
    server: null,
    _server: null,
    columns: 80,
    rows: 24,
    _type: 'tty',
    fd: 1,
    _isStdio: true,
    destroySoon: [Function: destroy],
    _destroy: [Function: dummyDestroy],
    [Symbol(asyncId)]: 3,
    [Symbol(kHandle)]: TTY { [Symbol(owner_symbol)]: [Circular] },
    [Symbol(kSetNoDelay)]: false,
    [Symbol(lastWriteQueueSize)]: 0,
    [Symbol(timeout)]: null,
    [Symbol(kBuffer)]: null,
    [Symbol(kBufferCb)]: null,
    [Symbol(kBufferGen)]: null,
    [Symbol(kCapture)]: false,
    [Symbol(kBytesRead)]: 0,
    [Symbol(kBytesWritten)]: 0
  },
  input: ReadStream {
    connecting: false,
    _hadError: false,
    _parent: null,
    _host: null,
    _readableState: ReadableState {
      objectMode: false,
      highWaterMark: 0,
      buffer: BufferList { head: null, tail: null, length: 0 },
      length: 0,
      pipes: null,
      pipesCount: 0,
      flowing: true,
      ended: false,
      endEmitted: false,
      reading: false,
      sync: false,
      needReadable: true,
      emittedReadable: false,
      readableListening: false,
      resumeScheduled: true,
      emitClose: false,
      autoDestroy: false,
      destroyed: false,
      defaultEncoding: 'utf8',
      awaitDrainWriters: null,
      multiAwaitDrain: false,
      readingMore: false,
      decoder: null,
      encoding: null,
      [Symbol(kPaused)]: false
    },
    readable: true,
    _events: [Object: null prototype] {
      end: [Array],
      pause: [Function],
      data: [Function: onData],
      keypress: [Function: onkeypress]
    },
    _eventsCount: 4,
    _maxListeners: undefined,
    _writableState: WritableState {
      objectMode: false,
      highWaterMark: 0,
      finalCalled: false,
      needDrain: false,
      ending: false,
      ended: false,
      finished: false,
      destroyed: false,
      decodeStrings: false,
      defaultEncoding: 'utf8',
      length: 0,
      writing: false,
      corked: 0,
      sync: true,
      bufferProcessing: false,
      onwrite: [Function: bound onwrite],
      writecb: null,
      writelen: 0,
      afterWriteTickInfo: null,
      bufferedRequest: null,
      lastBufferedRequest: null,
      pendingcb: 0,
      prefinished: false,
      errorEmitted: false,
      emitClose: false,
      autoDestroy: false,
      bufferedRequestCount: 0,
      corkedRequestsFree: [Object]
    },
    writable: false,
    allowHalfOpen: false,
    _sockname: null,
    _pendingData: null,
    _pendingEncoding: '',
    server: null,
    _server: null,
    isRaw: true,
    isTTY: true,
    fd: 0,
    [Symbol(asyncId)]: 2,
    [Symbol(kHandle)]: TTY { reading: false, [Symbol(owner_symbol)]: [Circular] },
    [Symbol(kSetNoDelay)]: false,
    [Symbol(lastWriteQueueSize)]: 0,
    [Symbol(timeout)]: null,
    [Symbol(kBuffer)]: null,
    [Symbol(kBufferCb)]: null,
    [Symbol(kBufferGen)]: null,
    [Symbol(kCapture)]: false,
    [Symbol(kBytesRead)]: 0,
    [Symbol(kBytesWritten)]: 0,
    [Symbol(keypress-decoder)]: StringDecoder {
      encoding: 'utf8',
      [Symbol(kNativeDecoder)]: <Buffer 00 00 00 00 00 00 01>
    },
    [Symbol(escape-decoder)]: Object [Generator] {}
  },
  historySize: 30,
  removeHistoryDuplicates: false,
  crlfDelay: 100,
  _prompt: 'Guess the number! (0-10): ',
  terminal: true,
  line: '',
  cursor: 0,
  history: [],
  historyIndex: -1,
  prevRows: 0,
  [Symbol(kCapture)]: false,
  [Symbol(kSubstringSearch)]: null,
  [Symbol(line object stream)]: undefined
}
1 Answers

Replit outputs the value of the last evaluated expression, which in your case is terminal.on('close', ...). The function call returns the terminal instance, and this is the "extra" output that you are seeing.

You can solve this by adding the line undefined; at the end.

Related