2

I am going to make a game in javascript , in which i will gives user a option to select given characters and make a valid english word , now the question is that how can i check in javascript that what user enters is a valid english word or not , suppose i give user following letters

  1. O
  2. D
  3. O
  4. G

Now possibly he can make three words from these characters God, Good or Dog .

Thanks in advance

EDIT

what about this guys

Typo.JS

Is this a good way to do so or not ?

Smartboy
  • 986
  • 6
  • 22
  • 37

5 Answers5

5

He could also conceivably write DO and GO. You need to use a dictionary of the allowed words, and check the answer against that.

var allowedWords = ['god','good','dog','do','go'];
var enteredWord = 'GOD';

if(allowedWords.indexOf(enteredWord.toLowerCase()) >= 0) {
   // match
}
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • well if i consider `do` as valid or anything which is legal then what ? in that case i have to make my own dictionary – Smartboy Jan 04 '13 at 11:47
2

Here you have english word MySql database. http://androidtech.com/html/wordnet-mysql-20.php

Now you need to implement server-side functionality.Will return true or false when word not exist. You can call service by Ajax using Jquery and check words in your game.

Norbert Pisz
  • 3,392
  • 3
  • 27
  • 42
1

Check this out: http://ejohn.org/blog/dictionary-lookups-in-javascript/

Look for Client side solution.

ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
1

If you have multiple questions, you can also validate the answer like this:

var gameDict = [
    { 'letters':['o','d','o','g'],  'words':['god','good','dog','do','go', 'goo']},
    { 'letters':['a','e','p'],      'words':['ape']},
    { 'letters':['p','n','e','t'],  'words':['pen', 'ten', 'net']}
]

// Returns `true` if the answer is valid, `false` if it's not.
function validateAnswer(questionNumber, answer){
    return gameDict[questionNumber].words.indexOf(answer.toLowerCase()) >= 0;
}

console.log(validateAnswer(0,'Good'));
// true
console.log(validateAnswer(1,'ap'));
// false

But yes, you will have to manually write a dictionary for your game, since aside from checking if the word is actually a English word, you will also have to check if your characters can make that word.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
0

As Thor suggested you need a dictionary (an array of strings that are the words you consider valid "english words").

But if you really want to check the input against any english word the "English Dictionary" the array of words would be realy huge and, as it would be loaded along the Javascript, the Javascript would become a file too big to download along with your web page.

In that case, you'd need a database (let say MySQL) to store the words and a script that runs on the server (let's say PHP) that you pass the word with an ajax call. The script would make a query on the DB to see if the word exists and give back the result to your javascript as the ajax response (1=english word 0=word not on the dictionary)

Paolo
  • 15,233
  • 27
  • 70
  • 91