Random Fortune Cookie generator using a .txt file for Message storage

I found a way to challenge myself with this project. I wanted to make a fortune cookie generator that takes a list of fortune cookie sayings and displays a message with lucky numbers.

I didn’t want to shortcut the messages by placing them directly into an array. So the messages were added to a .txt file which i spent a deal of time trying to read and add to an array. I was finally able to get the program to work where it takes 1 line from the .txt and adds it to a formatted statement. if there’s an easier way to do what i did please let me know.

** Hi Ajax, just checked out your project and I’m curious where did you find resources how to access that text file, I was curious if we we can write on it in the same way? I am currently on the same project and I want to make “Happy Jar” in such way that every day for 360 days a year to display a random message a nice one, for example: “Failure is Success if you learn from it” . Good luck going forward. **

I found the website File System | Node.js v10.24.1 Documentation had what i needed then after some googling i found the code for

const fs = require(‘fs’); (the way i understood this imports the node ‘fs’ and the rest converts it to a string then splits it by the line break)

const text = fs.readFileSync(‘filelocation’).toString(‘utf-8’);
let textByLine = text.split(‘\n’);

1 Like

Thanks, good luck forward.