Using a text file as a database?

Hello,

I want to improve my random quote machine. Instead of hard-coding my quotes into the js file, I want to list them all in a text file. I thought this would make it easier for me to continuously add quotes to the program without changing any of the code itself.

Is this a good idea? I’ve been reading into FileReaders and thought that I could use a FileList to go through the quotes. I structured the text file so the quote is always on a single line with the author on the line below it, followed by a new line. I would not use the entire list for one user. Let’s say I have about 50 quotes (probably more), I would only grab 10 of them randomly from the text file and insert them into the program.

My only issue I keep thinking of is how to keep the program from choosing the same quote more than once within the 10 to pick. In the hard-coded version, I had a boolean value for each quote that determined whether its quote was already displayed.


I appreciate any feedback or better alternatives to this idea. I just want to have a way for a user to see all the new quotes I keep collecting.

Thank you!

When you load the file, add quotes into array. After that you can just generate random number from 0 to array length -1. Then display the quote with generated index and simply remove element from array. This way you will display every quote only once.

That’s a nice idea. I’d suggest using JSON syntax for your files and reading the data using AJAX methods of JQuery instead of FileReaders.

As for the randomness you can still have a boolean value for each of the quotes inside the JSON object or simply remove each one that was displayed once as suggested.

1 Like