

You can use code like the following to read in the contents of a JSON file: You can check if the value returned by your prompt() call is a string, and if not, execute the return command to exit the app gracefully. This is likely to make Node throw an error before it quits. You can add an explicit case in your switch statement that will execute the return command.īut what if the user presses ^C? In this case, the prompt-sync module returns the value null, and not a string.

By default, this will be an empty object: loop, the app will exit. Your can find the official documentation here.įor JavaScript files, require reads the file, executes it, and then returns a module.exports object. It takes a path to a JS or JSON file as its argument, and returns an object. The require function is built in to Node.js. This allows you to read a line of text from the Terminal into your a script that is running in Node.js This Node function allows you to read data from one file to use in another. With the help of a couple of simple new concepts, you can create an interactive app that runs in a Terminal window. You can use these word lists, or you can find lists of words in another language and use those words instead. The words in each list are chosen from the most frequently used words in a corpus based on Project Gutenberg. This directory contains 15 files with names like 2-letter-words.json to 16-letter-words.json. This repository contains a directory named word_lists. These new techniques are also explained below. The index.js and word_search.js scripts already handle the two unfamiliar techniques for you. You have already met all but two of the JavaScript techniques that you will need to write a similar app. Test each of the commands (and also test what happens if you type non-existant commands), to see what output is printed in the Terminal window. * h Prints a concise help message with no examples. * help Prints this Help message, complete with examples.
