Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

So, I am writing some javascript in a website project... The goal is to have thi

ID: 3668068 • Letter: S

Question

So, I am writing some javascript in a website project... The goal is to have this "spell-checker" search not only in the small array "dictionary" that I have, but also search the misspelled word for transposed letters. For instance, if someone typed "modnay" instead of "monday" the spell checker would suggest "monday" This is what I have so far:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Spell Check</title>
<script>
var dictionary = ["monday", "tuesday", "wednesday"];
function SpellCheck()
{
   var newWord = document.getElementById("word").value; //input
   for(var i=0; i<dictionary.length; i++)
       {
           if (newWord == dictionary[i])
           {
               //output found correct spelling and quit searching
               document.getElementById("output").value = newWord + " is spelled correctly. ";
               return;
           } //end if found          
       } //end for loop to print
  
   for(var i=0; i<newWord.length-1; i++)
   {
       var transWord = newWord.substring(0,i)
           +newWord.charAt(i+1)
           +newWord.charAt(i)
           +newWord.substring(i+2)
          
       for(var j=0; j<dictionary.length; j++)
       {
           if (transWord == dictionary[j])
           {
               document.getElementById("output").value = "Did you mean " + transWord;
               return;
       } //End search newly created trans word
   } //end search for transposed letters
      
       document.getElementById("output").value = newWord + " is not found! ";
} //end SpellCheck function
  
function AddWord()
{
   dictionary.push(document.getElementById("word").value);
} //end AddWord function
</script>
</head>

<body>
Word: <input type="text" id="word"><br><br>
<button>Spell Check</button><br><br>
<button>Add Word</button><br><br>
<textarea rows="10" cols="20" id="output">
Type a word and click the "Spell Check" button.
</textarea>
</body>
</html>

You can see the variable transWord is set up to detect trasposed letters, but instead it is throwing an error in any browser in which I try to run it. Suggestions?

Explanation / Answer

Use Bellow code working fine.Plz let me know your comments.

<html>
<head>
<meta charset="utf-8">
<title>Spell Check</title>
<script>
debugger;
var dictionary = ["monday", "tuesday", "wednesday"];
function SpellCheck() {

var newWord = document.getElementById("word").value; //input
for (var i = 0; i < dictionary.length; i++) {
if (newWord == dictionary[i]) {
//output found correct spelling and quit searching
document.getElementById("output").value = newWord + " is spelled correctly. ";
return;
} //end if found
} //end for loop to print

for (var i = 0; i < newWord.length - 1; i++) {
var transWord = newWord.substring(0, i)
+ newWord.charAt(i + 1)
+ newWord.charAt(i)
+ newWord.substring(i + 2)

for (var j = 0; j < dictionary.length; j++) {
if (transWord == dictionary[j]) {
document.getElementById("output").value = "Did you mean " + transWord;
return;
} //End search newly created trans word
} //end search for transposed letters

document.getElementById("output").value = newWord + " is not found! ";
} //end SpellCheck function


}
function AddWord() {
  
dictionary.push(document.getElementById("word").value);
document.getElementById("output").value = "Word Added to dictionary";
}
</script>
</head>
<body>
Word: <input type="text" id="word"><br><br>
<button>Spell Check</button><br><br>
<button>Add Word</button><br><br>
<textarea rows="10" cols="20" id="output">
Type a word and click the "Spell Check" button.
</textarea>
</body>
</html>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote