I’m creating a Wordle-like game and I need to create a dictionary in a TXT file. At the moment I’m just using a vector in my script.js file.
The idea is to have one word on each line like this:
ANGEL
APPLE
HELLO
...
I created the .txt and am reading it from my JS
import { readFile } from "fs";
readFile("dictionary.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
const randomLine = lines[Math.floor(Math.random() * lines.length)];
console.log(randomLine);
});
2
Answers
This code reads the dictionary.txt file, splits ("n") its content into lines, selects a random line, and logs it to the console.
I am not sure if there is real necessity to deal with file and store as text. I would recommend changing file structure to json and have array there.
Example: