Input and Output

Memory vs I/O

Terminal I/O

process.stdin.once('data', (chunk) => { console.log(chunk.toString()) } )

The weirdness is explained on the next slide!

node load code, decoded

process.stdin.once('data',
    (chunk) => { console.log(chunk.toString()) }
)

once is a function that takes two parameters, and its second parameter is another function

phrase meaning
process.stdin hey terminal input,
.once('data', ... ) when you get some data,
(chunk) please name it chunk
=> and send it to
{ ... } this block of code
console.log(chunk.toString()) convert it to a string and print it to the terminal

Welcome to Callback City!

The previous one-liner code is equivalent to this:

function printLine(chunk) { 
    console.log(chunk) 
}
process.stdin.once('data', printLine);

The printLine function itself is called a callback (since you are asking the I/O device to call you back when it receives input).

LAB: Hello, friend!

  1. Open hello.js in your text editor
  2. Change it to contain the following code:
console.log("What is your name?");
process.stdin.once('data', (chunk) => {
    let name = chunk.toString();
    console.log("Hello, " + name + "!");
});
  1. Save the file and switch back to the terminal
  2. Run the program using node hello.js
  3. Type in your name and press the Return key (also called Enter)

What happens? Is this what you expected?

Yikes!

Control-C to close

Let's fix this

The newline character

Trim it

Solution: fixing Hello, Friend

console.log("What is your name?");
process.stdin.once('data', (chunk) => {
    let name = chunk.toString().trim();
    console.log("Hello, " + name + "!");
});

This Way To The Exit

console.log("What is your name?");
process.stdin.once('data', (chunk) => {
    let name = chunk.toString().trim();
    console.log("Hello, " + name + "!");
    process.exit();
});

Note that:

LAB: Capitalization

Hint: remember slice from the Strings lesson?

YELL YOUR NAME!

Okay, that was a bit much. Let's go back to just having our name capitalized.

readline

using readline

Warning: this code uses features we have not yet covered! Copy and paste it verbatim during the codealong below, and don't worry if it doesn't make much sense yet.

To use readline, include the following lines in the top of your source file:

const readline = require('readline');
const readlineInterface = readline.createInterface(process.stdin, process.stdout);

function ask(questionText) {
  return new Promise((resolve, reject) => {
    readlineInterface.question(questionText, resolve);
  });
}

using readline - explanation

code explanation
const readline = require('readline'); load the readline package and name it readline
const readlineInterface = readline.createInterface({...}) create an interface to readline using the following settings:
process.stdin, for input, use the standard input stream (i.e. terminal keyboard input)
process.stdout for output, use the standard output stream (i.e. terminal console output)
function ask(questionText) {...} a function named ask that uses the Promise API to asynchronously ask a question and wait for a reply

(We will cover the Promise API in much more detail later; for now, all you really need to know is that Promises allow us to use async and await in the next slide.)

LAB: using readline and await

Codealong time! Please follow along with the instructor and enter this code into a file named quest.js:

const readline = require('readline');
const readlineInterface = readline.createInterface(process.stdin, process.stdout);

function ask(questionText) {
  return new Promise((resolve, reject) => {
    readlineInterface.question(questionText, resolve);
  });
}

start();

async function start() {
  let name = await ask('What is your name? ');
  let quest = await ask('What is your quest? ');
  let color = await ask('What is your favorite color? ');
  console.log('Hello ' + name + '! ' +
    'Good luck with ' + quest + ', ' +
    'and here is a ' + color + ' flower for you.');
  process.exit();
}

async and await

1. `await` means "wait for the following thing to happen"
2. when you use `await` inside a function, you must use `async` to define that function

WARNING: async functions don't play nicely with for loops! (Fortunately, there are other ways to loop that do work well.)

LAB: Full Name

Full Name solution

Hint You may want to use readline to accept user input asynchronously
const readline = require('readline');
const readlineInterface = readline.createInterface(process.stdin, process.stdout);

function ask(questionText) {
  return new Promise((resolve, reject) => {
    readlineInterface.question(questionText, resolve);
  });
}

Solution
const readline = require('readline');
const readlineInterface = readline.createInterface(process.stdin, process.stdout);

function ask(questionText) {
  return new Promise((resolve, reject) => {
    readlineInterface.question(questionText, resolve);
  });
}

async function fullName() {
  let prenomen = await ask("What is your first name? ")
  let cognomen = await ask("What is your last name? ")

  console.log("Hello, " + prenomen + " " + cognomen + "!")
}

Lab: Name Length

Lab: Madlibs

Let's create a Madlib template! The goal for this lab is to create a program that will ask for certain types of words (noun, verb, adjective, etc.) and once it has recieved a certain number of inputs it will insert those words, in the correct places, into a story which your computer will print to the terminal.

 Previous Next 

Outline

[menu]

/