I'm trying to create my own lorem ipsum app and I want to keep my code clean by storing my word bank in other files. How can I access an array stored in a different JS file? For example, instead of hardcoding harry = ["", "", ""], I want to store that data in a different file and just call that file into the array.
// generator.js
function GenerateNewText(){
this.sentences = [
harry = [
"I am the chosen one!",
"Boy wizard",
"Quidditch seeker"
ron = [
"I am a Weasley",
"Gryffindor",
"Quidditch keeper"
]
]
}
GenerateNewText.prototype.getRandomSentence = function() {
let randomSentence = this.sentences[0][Math.floor(Math.random() * this.sentences.length)]
return randomSentence;
}
Currently, I have a harryText.js which contains
// harryText.js
harryText = [
"I am the chosen one",
"I am a Gryffindor",
"I am a boy"
]
module.exports = harryText;
but doing this in my generator.js shows harryText is not defined
function GenerateNewText(){
this.sentences = [
harryText, <---- error here
ron = [
"I am a Weasley",
"Gryffindor",
"Quidditch keeper"
]
]
}
I tried requiring it like so
const harryText = require("./harryText.js")
and the problem persists. I'm guessing a scope issue?
I tried installing ejs and changing harryText.ejs and including it like <%= include harryText %> in the generator array and that's invalid code.
Is calling an array from another file and storing it within another array even possible? Does anyone know a solution to this?
And yes, I know a Harry Potter Ipsum already exists. This is just dummy text.
harryTextvariable inharryText.js. You just started using it without sayingconstorletfirst.