I made a script that gives you three fortunes based on the last letter of your first name

Here’s the link to my GItHub with the project: GitHub - thaddeus0605/mixed_messges

I wanted to do something a little more involved that just a random message generator.

This script will:
-take in your first name
-capitalize it to account for different input methods
-saves the last name to a variable
-takes that and matches it to a fortune object with an array of 3 fortunes for each letter of the alphabet
-logs the fortunes to the console

I had to do some research to figure out how to get user input using NodeJs as using just a basic “prompt()” call was not working in Node.

You do need to have the ‘read-line sync’ package for node installed to get user input, before the program will ask you for your input.

Let me know what you all think!



Some thoughts and recommendations:

  1. Read about npm and use it. So you will not need to mention dependencies in your README - they will be in package.json file. Quickstart with npm:
    1.1. Create new package by executing npm init -y command in your project folder. It will create package.json file with autogenerated information about your project. You can change some fields, see package.json | npm Docs for reference.
    1.2. Install readline-sync package by npm install readline-sync. This package will be added to dependencies field of package.json.
    1.3. Add start command to scripts section of package.json file like this "start": "node script.js", .
    1.4. Exclude node_modules folder from git by creating .gitignore file with single line node_modules/. See also great service for generation gitignore files gitignore.io - Create Useful .gitignore Files For Your Project
    What you get in result: to run your script anyone can use npm install to install dependencies and npm start to run. More about npm: About npm | npm Docs
  2. You can optimize your code by access fortunes by key-index like fortunes[lastLetter]. So you do not need huge switch ... case.
  3. I recommend to check is last symbol realy is letter. It may be whitespace or any other symbol. You can trim string before getting last letter and then check if last symbol is letter.
  4. For the future: store you fortunes in text file or database. So you do not need to change your code when adding new fortunes.

Nice work!