...

/

Solution: Turning a List of Phrases Into an Array of Strings

Solution: Turning a List of Phrases Into an Array of Strings

Let's look at the solutions to the exercise given in the previous lesson.

We'll cover the following...

Solution #1

Alright, ready on your side?

The first solution we could think of is the following:

Press + to interact
index.js
sample.txt
const fs = require("fs")
function regExparser(txt) {
let arrContent = txt.replace(/(.+)/g, '"$1",')
let myCode = `let myArray = [${arrContent}]`
return myCode
}
fs.readFile("./sample.txt", "utf8", (err, content) => {
if(err) console.error(err)
console.log(content.toString())
console.log(regExparser(content.toString()))
})

Assuming a text file with the exact same content as the one shown on the diagram above, here is the output that we get:

Notice how ...