0

I am trying to form a word list in Javascript, whereby each word is a key in the dictionary and the value is true. My program takes a word and and checks whether the word is a valid word in the list or not. The list of words is stored in a text file where words are separated by new line. I scan these words as a string and split the string to obtain an array of strings which consist of all words. Now I am unable to form the associative array from this array. Here is my code:

var dict={};
var words;
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }


xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
words=xmlhttp.responseText.split("\n");
for ( var i = 0; i < words.length; i++ ) {
       dict[words[i]] = true; //This Section of Code not working
}
}
 }


xmlhttp.open("GET","twl06.txt",true);
xmlhttp.send();
}
function check(str)
{
if(dict[str])
    alert("Correct");
else
    alert("Incorrect");
}

After doing a lot of tweaks with the code I have figured out that somehow the assosciative array is not being formed.

Here is the link to my full html/JS code: https://pastebin.com/2jwMcBfA

11
  • How do you know that dict[words[i]] = true; "does not work" and what do you even mean by does not work? The line looks correct, assuming that words does indeed contain a list of words. Do you get any error? You have to provide more information. Do some basic debugging, inspect the values of the variables, etc. Commented Jan 24, 2012 at 11:40
  • @FelixKling Kling :The words array is correctly being generated as I can access the individual elements by using a subscript notation. Now my first word is "AA" . If i do words[0], I get "AA". But if i do dict["AA"] after running the above function, I get undefined. Commented Jan 24, 2012 at 11:45
  • 1
    Maybe you are accessing dict before it was populated. Ajax is asynchronous, so the code following your loadXMLDoc() call is run before the onreadystatechange handler is executed. Another problem could be that the words actually have leading or trailing white spaces. Make sure you use console.log to inspect the data and not alert. Commented Jan 24, 2012 at 11:48
  • @FelixKling: Words do not have a trailing or leading space i checked that. Also if I replace the for loop for dict with simply dict["AA"]=true in loadXMLDoc function,then the function check works for "AA" Commented Jan 24, 2012 at 11:59
  • Well, without a demo or the actually data is not much we can say. Commented Jan 24, 2012 at 12:02

2 Answers 2

1

When you split I would sugest that you split on \r\n not only \n that will leave a \r trailing

-1

Unless words[i] is an integer, you cannot use it as index for another array (JavaScript does not have associative arrays).

Best workaround is to do dict[words.indexOf(i)] /(edited)/ or some other way to convert words[i] to integer.

4
  • The Problem has been solved by the suggestion of David Labarge Commented Jan 24, 2012 at 13:02
  • dict is an object, words[i] will be a string. It's perfectly fine to set properties this way. indexOf is a function that does not exist. Commented Jan 24, 2012 at 13:22
  • Sorry, my mistake, it should have been dict[words.idexOf(i)]. And although you can use a string as index, it's not quite easy to parse the entire array later (indexOf('string') returns -1). indexOf DOES exist: link Commented Jan 24, 2012 at 15:07
  • Again, dict is an object not an array. Objects don't have an indexOf method. It would not make sense, has properties have no order. The whole purpose of using an object here is to have a fast lookup of words. Of course you should never use string indexes with an array, but that is not the case here. Commented Jan 24, 2012 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.