DéJSà Vu

Although we’ve only used JavaScript client-side, it can also be used server-side. (Indeed, it’s a popular alternative to Python for web apps!) Rather than rely on a browser to execute JavaScript code, you can instead use Node.js, an “open-source, cross-platform JavaScript runtime environment.” For instance, log into code.cs50.io and create a file called hello.js with just this code, wherein console.log is a function, a la print in Python, that “logs” (i.e., prints) a string to your “console” (i.e., terminal window):

console.log('hello, world');

Then, execute:

node hello.js

Hello, server-side JavaScript!

Now, it isn’t quite as easy in JavaScript, as in Python, to get user input, as JavaScript functions are often asynchronous, whereby, instead of returning a value synchronously (i.e., right away), they instead call a callback function when they’re ready to return a value, passing that value to that function as an argument.

Consider, for instance, nodejs.dev/en/learn/accept-input-from-the-command-line-in-nodejs, wherein the first example uses Node.js’s built-in readline library. After initializing the library for input and output via a function called createInterface, the example then prompts the user for input via a function called question. But question does not return a value itself, as does input in Python; question instead takes, in the example, an anonymous function as its second argument (a la lambda in Python), which it calls when it’s ready to return a value, passing that value into that anonymous function as its sole argument.

While harder to use, asynchronous functions essentially allow JavaScript to (appear to) execute multiple functions at once. For instance, while waiting for user input, JavaScript can execute some other function in parallel.

However, synchronous alternatives sometimes exist, as via third-party libraries, which can be easier, if less efficient, to use. For instance, consider the code below, which instead uses a third-party readline-sync library, whose own implementation of question synchronously returns the user’s input, per npmjs.com/package/readline-sync:

const readlineSync = require('readline-sync');

let name = readlineSync.question("What's your name? ");
console.log('hello, ' + name);

You can install readline-sync for use within your current directory with:

npm install readline-sync

This is the only library you need to solve the below questions!

  1. (4 points.) In test/js/mario.js, implement the more-comfortable version of Mario from Problem Set 6, this time in JavaScript instead of Python, using the synchronous version of question from readline-sync.

  2. (4 points.) In test/js/cash.js, implement Cash from Problem Set 6, this time in JavaScript instead of Python, using the synchronous version of question from readline-sync.

  3. (4 points.) In test/js/readability.js, re-implement Readability from Problem Set 6, this time in JavaScript instead of Python, using the synchronous version of question from readline-sync.

  4. (4 points.) In test/js/async.js, re-implement Hello from Problem Set 6, this time in JavaScript instead of Python, using the asynchronous implementation of question from readline rather than readline-sync.