Jump to content

Help with arrays


BigAl75

Recommended Posts

I'm new to javascript, and was wondering how I would go about validating a word submitted by a user against an existing array? I want the word the user submits to match one of the 5 words in the array I created. I'm guessing I'd have to use something like array.toString(), wordSubmitted.split(""), arrayWord1.split(), then compare the two, if that doesn't match, move to the second arrayWord. Am I correct in this?Al

Link to comment
Share on other sites

var arr = new Array();arr[0] = "word1";arr[1] = "word2";arr[2] = "word3";arr[3] = "word4";arr[4] = "word5";var userword = document.getElementById('textboxId').value;var i;for(i=0;i<arr.length;i++){  if(userword == arr[i])  {     alert('match found');     //do something  }}

Link to comment
Share on other sites

I'm guessing I'll have to use something like this to get double letter posts and misplaced letters to be checked, correct?

<script type="text/javascript">	<!-- This creates individual strings from the array elements and prints the length of each element in the array	var wordList = new Array()  wordList[0] = "their";  wordList[1] = "calendar";  wordList[2] = "cemetery";  wordList[3] = "recommend";  wordList[4] = "believe";	var v = wordList[0].toString();	var w = wordList[1].toString();	var x = wordList[2].toString();	var y = wordList[3].toString();	var z = wordList[4].toString();		var vSplit = v.split("");	var wSplit = w.split("");	var xSplit = x.split("");	var ySplit = y.split("");	var zSplit = z.split("");		--></script>

Link to comment
Share on other sites

What about double letters and misspelled words?  Like ttheir and thier?

It's not your fault if numpty's cant spell :) Put in large bold text that they must get the spelling correct for it to work, otherwise you would have to code for tons of possibilities...
Link to comment
Share on other sites

Heh. I don't have much of a choice. The reason I'm asking how to do it instead of asking if there was someone who could do it for me is that it's an assignment for a college class.I have to create a 5 word array (the dictionary) and check a user submitted word against that array. If the word doesn't match right off, I then have to check for transposed letters ( hteir instead of their) and double-posted letters (ttheir instead of their). If the word does not match, I have to give either the correct spelling of the word, or else ask the user if they want to add the word (I'm guessing using an array.push() method) to the dictionary.Here's what I have so far. I keep getting the error "form2 has no properties" (I've highlighted the line that FireFox's JavaScript Console tells me the error is in, but don't know how to fix it.):

<script type="text/javascript">	<!--  function readText (form)   { 	 var wordList = new Array()    wordList[0] = "their";    wordList[1] = "calendar";    wordList[2] = "cemetery";    wordList[3] = "recommend";    wordList[4] = "believe"; 	 var wordUsed = form.inputbox.value; 	 var x;    for(x=0; x<wordList.length; x++)    {      if(wordUsed == wordList[x])      {          form.wordCompareResult.value = "Your word is spelled correctly";      break;      }      else if(wordUsed != wordList[x])      {     	 form.wordCompareResult.value = "Your word is NOT spelled correctly, or is NOT in the dictionary.  If you\'re sure it is spelled correctly " +      "you can add it to the dictionary by pressing the \'Add Word\' button.";	      }    } 	   }  function addWord (form2)  { 	 var userSubWord = new Array() 	 userSubWord.push() 	 var s = userSubWord.toString();/*********************HERE*******************************//*------------------------------------------------------*//*------------------------------------------------------*/ 	 form2.inputbox2.value = s;/*------------------------------------------------------*//*------------------------------------------------------*//*********************HERE*******************************/ 	 alert(s);  }  --></script></head><body><form name="form" method="get">Enter your word: <br /><input type="text" name="inputbox" /><input type="button" name="button2" value="Check Spelling" onClick="readText(this.form)"><input type="submit" name="Submit" value="Add Word" onclick="addWord ()" />  <p>    <textarea name="wordCompareResult" cols="60" rows="6" id="arrayObjects" readonly="true"></textarea>  </p><p>    <input type="reset" name="Reset" value="Clear All fields" />  </p></form><form name="form2" method="post">  <p>    <input type="text" name="inputbox2" readonly="true">  </p></form>

Link to comment
Share on other sites

I need to exchange information between functions. I have 2 functions; wordList and addWord. The wordList function is doing almost all of the work. All I want to do with the addWord function is to add the user submitted word to the end of the array created in wordList so that, it will also be in the dictionary. Once this happens, the user should be able to try checking the word again, and it should say that the word is spelled correctly.I am at a complete loss on how to do this. I keep getting a "wordList has no properties" error or a "wordList not defined" error.Here's what I got so far (javascript code on its own page, form code on html page):

function readText (form)   { 	 <!-- This creates individual strings from the array elements and prints the length of each element in the array	var wordList = new Array()  wordList[0] = "their";  wordList[1] = "calendar";  wordList[2] = "cemetery";  wordList[3] = "recommend";  wordList[4] = "believe";  wordList[5] = "x";	var v = wordList[0].toString();	var w = wordList[1].toString();	var x = wordList[2].toString();	var y = wordList[3].toString();	var z = wordList[4].toString();		var vSplit = v.split("");	var wSplit = w.split("");	var xSplit = x.split("");	var ySplit = y.split("");	var zSplit = z.split("");		/*  document.writeln((v.length) + ", " + v + ", " + vSplit + "<br />")  document.writeln((w.length) + ", " + w + ", " + wSplit +" <br />")  document.writeln((x.length) + ", " + x + ", " + xSplit + "<br />")  document.writeln((y.length) + ", " + y + ", " + ySplit + "<br />")  document.writeln((z.length) + ", " + z + ", " + zSplit +" <br />")	*/	-->	var wordUsed = form.inputbox.value;	var x;  for(x=0; x<wordList.length; x++)  {    if(wordUsed == wordList[x])    {        form.wordCompareResult.value = "Your word is spelled correctly";    break;    }    else if(wordUsed != wordList[x])    {   	 form.wordCompareResult.value = "Your word is NOT spelled correctly, or is NOT in the dictionary.  If you\'re sure it is spelled correctly " +    "you can add it to the dictionary by pressing the \'Add Word\' button below.";	    }  }		}function addWord()	{  var a = wordList[5].toString(); 	 form.inputbox2.value = a;	  wordList.push(wordUsed)  wordList.splice(4,wordUsed)	}

<form name="form" method="get">Enter your word: <br /><input type="text" name="inputbox" /><input type="button" name="button2" value="Check Spelling" onClick="readText(this.form)">	<input type="button" name="addword" value="Add Word" onclick="addWord()" />  <p>    <textarea name="wordCompareResult" cols="60" rows="6" id="arrayObjects" readonly="true"></textarea>  </p><p>    <input type="reset" name="Reset" value="Clear All fields" />  </p>  <p>    <input type="text" name="inputbox2">  </p></form>

And I still need to figure out how to check for missplaced letters (hteir) and double typed letters (ttheir). I have an idea on how I will go about this, but I just can't figure out how exactly to do it. I came up with this to split up the array to strings, then the strings to individual letters:

var wordList = new Array()  wordList[0] = "their";  wordList[1] = "calendar";  wordList[2] = "cemetery";  wordList[3] = "recommend";  wordList[4] = "believe";	var v = wordList[0].toString();	var w = wordList[1].toString();	var x = wordList[2].toString();	var y = wordList[3].toString();	var z = wordList[4].toString();		var vSplit = v.split("");	var wSplit = w.split("");	var xSplit = x.split("");	var ySplit = y.split("");	var zSplit = z.split("");		//not needed - for validation purposes only	document.writeln((v.length) + ", " + v + ", " + vSplit + "<br />")	document.writeln((w.length) + ", " + w + ", " + wSplit +" <br />")	document.writeln((x.length) + ", " + x + ", " + xSplit + "<br />")	document.writeln((y.length) + ", " + y + ", " + ySplit + "<br />")	document.writeln((z.length) + ", " + z + ", " + zSplit +" <br />")

Any ideas? I know it's kind of last minute, as it's due Friday (2/17) but I'd really appreciate any help.Thanks.Al

Link to comment
Share on other sites

Well, it almost kinda sorta works. This is what I have for javascript code:

var wordList = new Array()  wordList[0] = "their";  wordList[1] = "calendar";  wordList[2] = "miscellaneous";  wordList[3] = "recommend";  wordList[4] = "believe";  wordList[5] = "x";  function readText (form) {/*This creates individual strings from the array elements*/	var v = wordList[0].toString();	var w = wordList[1].toString();	var x = wordList[2].toString();	var y = wordList[3].toString();	var z = wordList[4].toString();	var u = wordList[5].toString();/* *//*This splits the above strings into individual letters*/	var vSplit = v.split("");	var wSplit = w.split("");	var xSplit = x.split("");	var ySplit = y.split("");	var zSplit = z.split("");	var uSplit = u.split("");	var b;	var c;	var wordUsed = form.inputbox.value;		for(c=0; c<wordList.length; c++) 	 {  	if(wordUsed == wordList[c]) 	 {   	 form.wordCompareResult.value = "Your word is spelled correctly.";    break; 	 }  else if(wordUsed != wordList[c]) 	 {   	 if ((v.length == wordUsed.length) && (wordUsed.substring(1) == v.substring(1))) 	 {	    for (b=0; b < v.length; b++)    {    if(wordUsed == wordList[b])    {       	 form.wordCompareResult.value = "Your word is spelled correctly";   	 break;    }      else if (wordUsed.substring(1) == v.substring(1))    {   	 form.wordCompareResult.value = "Perhaps you meant " + v + "?";    }    else if (wordUsed.substring(2) == v.substring(2))    {   	 form.wordCompareResult.value = "Perhaps you meant " + v + "?";    }    else if (wordUsed.substring(3) == v.substring(3))    {   	 form.wordCompareResult.value = "Perhaps you meant " + v + "?";    }    else if (wordUsed.substring(4) == v.substring(4))    {   	 form.wordCompareResult.value = "Perhaps you meant " + v + "?";    }    else if (wordUsed.substring(5) == v.substring(5))    {   	 form.wordCompareResult.value = "Perhaps you meant " + v + "?";    }    else if (wordUsed != v)     	 {    form.wordCompareResult.value = "The word you entered could not be found in the dictionary";    } 	 }  }	else if ((w.length == wordUsed.length) && (wordUsed.substring(1) == w.substring(1)))	{ 	 for (b=0; b < w.length; b++) 	 {    if(wordUsed == wordList[b])    {        form.wordCompareResult.value = "Your word is spelled correctly";   	 break;    }        else if (wordUsed.substring(1) == w.substring(1))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }    else if (wordUsed.substring(2) == w.substring(2))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }    else if (wordUsed.substring(3) == w.substring(3))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }	    else if (wordUsed.substring(4) == w.substring(4))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }    else if (wordUsed.substring(5) == w.substring(5))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }    else if (wordUsed.substring(6) == w.substring(6))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }    else if (wordUsed.substring(7) == w.substring(7))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }    else if (wordUsed.substring(8) == w.substring(8))    {   	 form.wordCompareResult.value = "Perhaps you meant " + w + "?";    }    else if (wordUsed != w)        {   	 form.wordCompareResult.value = "The word you entered could not be found in the dictionary";    } 	 }  }	else if ((x.length == wordUsed.length) && (wordUsed.substring(1) == x.substring(1)))	{ 	 for (b=0; b < x.length; b++) 	 {     	 if(wordUsed == wordList[b])    {        form.wordCompareResult.value = "Your word is spelled correctly";   	 break;    }        else if (wordUsed.substring(1) == x.substring(1))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(2) == x.substring(2))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(3) == x.substring(3))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(4) == x.substring(4))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }	    else if (wordUsed.substring(5) == x.substring(5))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(6) == x.substring(6))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }	    else if (wordUsed.substring(7) == x.substring(7))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(8) == x.substring(8))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(9) == x.substring(9))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(10) == x.substring(10))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(11) == x.substring(11))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(12) == x.substring(12))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed.substring(13) == x.substring(13))    {   	 form.wordCompareResult.value = "Perhaps you meant " + x + "?";    }    else if (wordUsed != x)    {   	 form.wordCompareResult.value = "The word you entered could not be found in the dictionary";    } 	 }	}	else if ((y.length == wordUsed.length) && (wordUsed.substring(1) == y.substring(1)))	{ 	 for (b=0; b < y.length; b++) 	 {    if(wordUsed == wordList[b])    {      form.wordCompareResult.value = "Your word is spelled correctly";      break;    }    else if (wordUsed.substring(1) == y.substring(1))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }	    else if (wordUsed.substring(2) == y.substring(2))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }	    else if (wordUsed.substring(3) == y.substring(3))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }	    else if (wordUsed.substring(4) == y.substring(4))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }	    else if (wordUsed.substring(5) == y.substring(5))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }    else if (wordUsed.substring(6) == y.substring(6))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }	    else if (wordUsed.substring(7) == y.substring(7))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }	    else if (wordUsed.substring(8) == y.substring(8))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }    else if (wordUsed.substring(9) == y.substring(9))    {   	 form.wordCompareResult.value = "Perhaps you meant " + y + "?";    }    else if (wordUsed != y)    {   	 form.wordCompareResult.value = "The word you entered could not be found in the dictionary";    } 	 }  }	else if ((z.length == wordUsed.length) && (wordUsed.substring(1) == z.substring(1)))  { 	 for (b=0; b < z.length; b++)    {     	 if(wordUsed == wordList[b])   	 {          form.wordCompareResult.value = "Your word is spelled correctly";      break;   	 }     	 else if (wordUsed.substring(1) == z.substring(1))   	 {      form.wordCompareResult.value = "Perhaps you meant " + z + "?";   	 }   	 else if (wordUsed.substring(2) == z.substring(2))   	 {      form.wordCompareResult.value = "Perhaps you meant " + z + "?";   	 }   	 else if (wordUsed.substring(3) == z.substring(3))   	 {      form.wordCompareResult.value = "Perhaps you meant " + z + "?";   	 }   	 else if (wordUsed.substring(4) == z.substring(4))   	 {      form.wordCompareResult.value = "Perhaps you meant " + z + "?";   	 }   	 else if (wordUsed.substring(5) == z.substring(5))   	 {      form.wordCompareResult.value = "Perhaps you meant " + z + "?";   	 }   	 else if (wordUsed.substring(6) == z.substring(6))   	 {      form.wordCompareResult.value = "Perhaps you meant " + z + "?";   	 }   	 else if (wordUsed.substring(7) == z.substring(7))   	 {      form.wordCompareResult.value = "Perhaps you meant " + z + "?";   	 }   	 else if (wordUsed != z)       	 {      form.wordCompareResult.value = "The word you entered could not be found in the dictionary";   	 }    } 	 } 	 else if ((u.length == wordUsed.length) && (wordUsed.substring(1) == u.substring(1)))	{ 	 for (b=0; b < u.length; b++) 	 {    if(wordUsed == wordList[b])    {      form.wordCompareResult.value = "Your word is spelled correctly";      break;    }    else if (wordUsed.substring(1) == u.substring(1))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }	    else if (wordUsed.substring(2) == u.substring(2))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }	    else if (wordUsed.substring(3) == u.substring(3))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }	    else if (wordUsed.substring(4) == u.substring(4))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }      else if (wordUsed.substring(5) == u.substring(5))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed.substring(6) == u.substring(6))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }		    else if (wordUsed.substring(7) == u.substring(7))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed.substring(8) == u.substring(8))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed.substring(9) == u.substring(9))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed.substring(10) == u.substring(10))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed.substring(11) == u.substring(11))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed.substring(12) == u.substring(12))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed.substring(13) == u.substring(13))    {   	 form.wordCompareResult.value = "Perhaps you meant " + u + "?";    }    else if (wordUsed != u)    {   	 form.wordCompareResult.value = "The word you entered could not be found in the dictionary";    } 	 }  } 	 else 	 {    form.wordCompareResult.value = "The word you entered is not in the dictionary."; 	 }  }	} 	 }function addWord()	{  wordList.splice(5,1,wordUsed)  form.inputbox2.value = wordList[5].toString();	}

and here's the html to go along with it:

<html><head><title>Spell checker for CIS 227</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><script src="spellcheck.js"></script></head><body><form name="form" method="get">Enter your word: <br /><input type="text" name="inputbox" /><input type="button" name="button2" value="Check Spelling" onClick="readText(this.form)">	<input type="button" name="addword" value="Add Word" onclick="wordList.push();" />  <p>    <textarea name="wordCompareResult" cols="60" rows="6" id="arrayObjects" readonly="true"></textarea>  </p><p>    <input type="reset" name="Reset" value="Clear All fields" />  </p>  <p>    <input type="text" name="inputbox2">  </p></form></body></html>

It was doing what it was supposed to do - check for misplaced letters (tehir instead of their) and returning a suggestion word. I added the

(wordUsed.substring(1) == u.substring(1))

because when you typed in "hello", instead of returning "The word you entered is not in the dictionary" it would recommend "their" because they both have 5 letters in them. Now, it says "The word you enetered....." for "hello", but it also returns "The word..." for tehir."I'm completely lost as to what to do next. I've been up until 3 or 4am every day for the last 18 days working on this, and it's due Friday (2/17). Like I said before, I don't want anyone to do it for me, just offer hints/suggestions as to how I can make it work. If I can at least get it back to suggesting words again, I'd be better than I am now.Thanks for any help. I'll check this thread again one more time before I face the terminator tomorrow. lolAl

Link to comment
Share on other sites

I tried going through the problem and kindof understood that you were trying to replicate the auto spell feature of microsoft word, where if one types a word, the application automatically corrects it ( if it was wrong and correctable ) or underlines it ( if wrong and uncorrectable and gives many could be correct options ) or does nothing to the word ( if it is correct ).I will take an example of the word : theirTry typing these and hitting return in microsoft word 2003...{1.} their ( nothing happens because it is correct ) {2.} thier ( it changes to their ) ---> you will notice that changing the places of two corrosponding alphabets will result in correction of the word by the application BUT given that neither of them are the first or last alphabets. i.e application will correct the misspelled words tehir and thier because the first and the last alphabets were same and the alphabets between the first and last had just changed places with their neighbour.........[ tehir e&h have changed places ] [ thier i&e have changed places ]. You will also note that if you change first and second alphabets ( t & h and type hteir ), the word will not be corrected because microsoft word assumes that at lease first and last alphabets are correct and in correct position.{3.} ttheir ( the word will be underlined and not corrected ) ---> but thheir will be corrected. So logically microsoft word assumes that the first alphabet should be correct and not duplicated. on the other hand the alphabets between the first and last and the last can be duplicated but the duplicated alphabets have to be side by side i.e. thheir,theeir,theiir and theirr will be corrected.Based on this, i can try to write a application which will { first } check if the word matches directly with the array elements. if not...{ second } assume that alphabet positions have been swapped with their neighbours. Here try swapping the alphabets within the word ( not the first and last alphabet ) with their neighbours and going through first step. example: for the word thier; try step one for [ tiher by swapping i & h ] and [ their by swapping i & e ]. if this too doesent work....{ third } assume that their is duplication of some alphabets ( but not the first one) . So for each duplicate alphabet within the word, remove one and go through first and second steps. Do the same for each duplication. if this doesent work.... give a list of words which have the maximum same number of alphabets and in the same position as the word given.I think i have written too much. But i hope i communicated my idea correctly. Ofcourse i may be wrong and even if i am right the application will use a lot of recursion. Their could be a simpler and fast solution. Kindly correct me where ever you think i am wrong. Today is 17 feb. So how was the terminator?? :) I am sure you would have come up with a good solution. :)

Link to comment
Share on other sites

It has to ask the user if they meant a word in the dictionary, not just auto-correct it. Other than that, you pretty much hit the nail on the head. It also has to be able to check for all misplaced letters (hteir, too).Right now it's only checking words that have the same number of letters. I added the (wordUsed.substring(1) == u.substring(1)) hoping to narrow it down, but that made it return "Your word is not in the dictionary" all the time unless the word was spelled correctly.I'm going to be messing with it right up to the last minute, as I have until midnight tonight (2/17) to get this turned in.Wish me luck, and than you all very much for your help.Al

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...