2

I am trying to parse text from a dictionary.txt file for a web browser word game. I found How to read a local text file? and used the readTextFile(file) function the top commentor suggested. However, I don't understand how to get the parsed text into a set.

function readTextFile(file)
    {
        var rawFile = new XMLHttpRequest();
        rawFile.open("GET", file, false);
        rawFile.onreadystatechange = function ()
        {
            if(rawFile.readyState === 4)
            {
                if(rawFile.status === 200 || rawFile.status == 0)
                {
                    var allText = rawFile.responseText;
                    alert(allText)
                }
            }
        }
        rawFile.send(null);
      }

alert(allText) gives me a popup with words in the dictionary.txt, so I know the words are getting properly parsed into the variable allText.

How do I now move these words into a set structure for use in my game? I am thinking I would do this in my main program--run readTextFile, then move to set.

Also, as this is my first time using JavaScript, when I run readTextFile in my main program, do I need to do:

myWords = readTextFile("dictionary.txt");

To store allText intomyWords, or will simply doing:

readTextFile("dictionary.txt");

Make it so that I can access allText in my main program? I am unfamiliar with scoping in JS.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Sky
  • 21
  • 1
  • So this is not an issue with retrieving the file, rather than parsing a list of words and adding them to a set... How many words do you want to have in this set? All of them? That may stress out the browser. Also, is `allText` (the response) a plaintext string delimited by new-lines? – Mr. Polywhirl Oct 30 '19 at 01:23
  • Hi, the only way to get a text file from the file system inside a browser is if the whole site is stored locally. Otherwise you will use the web standard practices to get the file. Some kind of ajax request for example. If there is nothing to parse you will not get any result. If you use node.js then you should also specify this in the question header. Node.js is allowed to read text files. So as you might see at the moment you confuses every ones :) – Thomas Ludewig Oct 30 '19 at 01:47

2 Answers2

0

I think that all you will need to do is split the text file by new-lines and send the resulting list and instantiate a new Set object with the mentioned list.

Also, check out the documentation on MDN for the Set object.

let myWords = readTextFile("dictionary.txt");

console.log(myWords.has('apple'));  // true
console.log(myWords.has('banana')); // true
console.log(myWords.has('carrot')); // true
console.log(myWords.has('durian')); // false

function readTextFile(filename) {
  // Blah, blah, blah...
  // Code to read the file and return a string...
  // Ok, now we have a result:
  let result =
`apple
banana
carrot
`;

  return new Set(result.split('\n')); // Split the text by new-line and instantiate
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

We will need to know the format of the file to help you parse it. If it is a text file with a word on each line, then you could return allText; in your function, then put it into an array using

var myWords=readTextFile('dictionary.txt').split("\n");
peekolo
  • 342
  • 3
  • 8