I was trying out new things in node, So here I want to read two files and check if all the words in the files are the same.
Example: Two similar files where one file has some extra words added to it.
So after checking both the files, if the words of one file don't exist in the other file, then those words should be printed out
Here is what I have tried so far, But this doesn't seem to work as expected and isn't the right way
let newData = [];
let oldData = [];
(async () => {
await new Promise(resolve => {
fs.readFile(
pathname.join(__dirname, "public", "hello.txt"),
(err, data) => {
const wordsInFile = data.toString().toLowerCase();
// Here we should split string by some delimiter for further words processing. Assume, that your file looks like this "word1 word2". We'll use space delimiter.
newData = wordsInFile.split(" ");
resolve();
}
);
});
// here is the same code for the second file. Or you can move code to another function.
await new Promise(resolve => {
fs.readFile(pathname.join(__dirname, "public", "test.txt"), (err, data) => {
const wordsInFile = data.toString().toLowerCase();
// Here we should split string by some delimiter for further words processing. Assume, that your file looks like this "word1 word2". We'll use space delimiter.
newData = wordsInFile.split(" ");
resolve();
});
});
// And now we can use Set from ES6, to find intersection/difference etc.
const wordsNotInFileAndNotInFileTwo = new Set(
[...newData].filter(x => !oldData.has(x))
);
console.log(wordsNotInFileAndNotInFileTwo);
// But you can simple iterate two arrays to achieve your goal.
})();
Any help would be appreciated