0

I am creating a game where I need to compare a word( that the player made by picking letters) to a word in a dictionary text file. I have already put all the words from the text file into an array.

var words = [];
$(document).ready(function()
{
    $.get("greaterthan3.txt", function(txt)
            {
                words = txt.split("\n"); //get array of all words

            });
});

Whenever I try to compare my created word(var completedWord = " ") with a word in my array, nothing happens. The following code is inside a button click function.

  if(completedWord == words[0])
  {
      alert("correct word");
  }

Is there any way to compare a word to a element in an array?

Thanks very much

///////////////////////////////////////////////////////////////////////////

Hi guys, this is my new code for my button click method.

$(function()
{
$("#playButton").on("click",function()
              {
                  for(var k = 0; k < words.length; k++)
                  {
                     if(completedWord == words[k])
                     {
                        alert("hi");
                     }

                  }

                  if($.inArray(completedWord,words) > -1)
                  {
                     alert(completedWord + " is in array");
                  }

              });

});

I have realized that when checking if my completedWord variable was in my array variable, the alerts are displayed if I type in the last word in my text file.

Is there any way to fix it so that it can check all the words in my array instead of just the last element of my array.

Thanks for the help.

John Whyte
  • 21
  • 1
  • 4

2 Answers2

2

I would recommend using the jQuery.inArray function to see if your words array contains the word guessed by the user.

var words = ["one", "two", "banana", "floor", "three"];
var guess = "banana";
if($.inArray(guess, words) > -1) {
    alert(guess + " is in the array!");
}

$.inArray(item, array) returns the index of item in array - if item doesn't exist in the array it returns -1.

Working example: http://jsfiddle.net/BZUET/12/

Anders Arpi
  • 8,277
  • 3
  • 33
  • 49
  • Any reason you'd recommend using `jQuery.inArray()` over the standard `Array.indexOf()` method like so `if(words.indexOf(guess) > -1) {...}`? – Anthony Grist Jul 26 '12 at 12:29
  • Yes - indexOf wont work in most Internet Explorer versions. :) Of course if you want to use the 'standard' method you can fix it: http://soledadpenades.com/2007/05/17/arrayindexof-in-internet-explorer/ – Anders Arpi Jul 26 '12 at 12:30
  • Thanks for the help, your example works even when I substitute guess for my completedWord but does not show the alert for when I insert in my words array from above with your words array. I set up words as var words = []; and then I do the above. Is there any reason why this doesn't work for me. Thanks for all the help from everyone. – John Whyte Jul 26 '12 at 13:24
  • Then you are doing something wrong, like the text file isn't formatted correctly. See a working example here: http://jsfiddle.net/BZUET/12/ – Anders Arpi Jul 26 '12 at 13:31
0

You can use either Hashtable.Contains() or Dictionary.ContainsValue() for this purpose.

Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55