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.