0

I'm currently trying to code a program which randomly generates 7-letter strings and then checks whether they're valid words in the English dictionary. I realise that I could create an array that has every word of the English language in it and then searches for the 7-letter string in that array - but, since that sounds pretty time-consuming, what would be a better way of doing this?

Thanks!

Zetland
  • 569
  • 2
  • 11
  • 25

2 Answers2

2

You don't need an Array, you'll need a HashTable, luckily for you Javascript objects are implemented as Hashtables, lookup should be much faster ( O(1) vs O(n) )than lookup in an Array. Search the web for an English Dictionary, make an object out of it and check if your string is in there. I see no other straightforward ways to test if a word is English. There are no logical tests you can do right ? being an English word by definition means that it is somewhere mentioned in 'the' English dictionary.

In the dictionary approach you'll probably won't match some conjugations of verbs, translation API will probably catch them.

Mathieu Borderé
  • 4,357
  • 2
  • 16
  • 24
  • That's correct for the purposes of my program. How do you make an object out of an online dictionary though? Sorry to ask this, I just can't find any explanation of this in the SERPs... – Zetland Sep 30 '15 at 11:43
  • Look in this thread http://stackoverflow.com/questions/4456446/dictionary-text-file you should be able to find a text file of an English Dictionary, and then do some research on how to make an object out of a text file – Mathieu Borderé Sep 30 '15 at 11:49
  • Thank you for answering and contributing to Stack Overflow, however please take a second look at the question, for it seems to be off-topic according to the [help/on-topic]. Answering off-topic question make it look like it's okay to ask such questions - it's not. Off-topic questions might get closed and then deleted, which would nullify your contribution! – Kyll Sep 30 '15 at 12:08
1

You can use Javascript objects as an array(associative array). So you can define your dictionary as a following object:

var dictionary = new Object();
dictionary["apple"] = "apple is a name of fruit";
dictionary["football"] = "football is a name of game played with 11 players."

And you can access it as :

alert(dictionary["apple"]); // which will print meaning of it.. in this case "apple is a name of fruit".

Hope it helps.

Girish Khadse
  • 35
  • 2
  • 11
  • Thanks. I'd have to write out the dictionary myself though? – Zetland Sep 30 '15 at 11:56
  • @Zetland Well, you can't get the solution fed directly into your browser's throat. – Kyll Sep 30 '15 at 12:07
  • 1
    Thank you for answering and contributing to Stack Overflow, however please take a second look at the question, for it seems to be off-topic according to the [help/on-topic]. Answering off-topic question make it look like it's okay to ask such questions - it's not. Off-topic questions might get closed and then deleted, which would nullify your contribution! – Kyll Sep 30 '15 at 12:07