Mixed Message Project - Pewdiepie Facts

Hi everyone, so this project is about generating random message and basically logs it on the terminal. I do not decide to make it interactive or bring it on the web page because I think I will do it later when I have enough experience and knowledge later on the course.

And everything so far is so good. But there is one problem I encounter when doing this project. In this project I have a text file called “facts.txt”. I want to read this file and put it into the array by splitting ‘newline’ character. Everything works fine until I notice that the data of the ‘facts’ variable outside the callback function is destroyed. Anyone knows the reason why and how can i store the data permanently inside the ‘facts’ array when I finish reading the text file.

I think the reason has something to do with deep-copy or something but it seems very confusing with javascript compared to something like C++.

The code I have problems with is as below:

const fs = require('fs'); let facts; fs.readFile('facts.txt', (err, data) => { if (err) throw err; let facts = data.toString().split('\n'); // console.log(facts); : it's ok here!!! console.log(generateMessage(facts)); }) // console.log(facts); undefined

my github project: GitHub - ThanhHieuDang0706/codecademy-mixed-message: Codecademy Portfolio Project
It woulld be very great if you guys can review my code too.
Thank you for helping.