How to validate user input against a dictionary

I’m working on a side project that is essentially a word search. What I want to do is when a user selects and submits a word the program needs to check that this is a valid english word. I could use advice on the best way to accomplish this.

I’ve considered using a word list like this: And having my program use a loop to search for a match between user input and the list. Issue that I see is that I’m not sure how efficient this would be. Additionally I’d have to somehow format the list as an array to search through and I’m not sure how to do that.

Any advice? :slight_smile:

Are you planning on using a back end for this project?

I was intending this to be a front end only project as I’ve only just completed that section… Unless it’s not possible?

For sure it’s possible. It just means that either you will need to have some kind of static dictionary file or you need to find some API that you can access safely with javascript.

One simple solution would be to copy and paste some dictionary into a JSON object and have javascript check it. Obviously there are a lot of words in a dictionary so be carefull how you check it else it might get slow.

Let’s say I go with a static dictionary built with a word list, such as the example I posted above. Would the best approach be to break the words down into 26 arrays based on their first letter. I could test the first letter of my user input then use a for loop for the corresponding array to see if there is a match. Or is this silly? Up until now my loops haven’t had to process this much information so I’m not sure the best way to approach this.

That would make sense to me. Also keep in mind that the dictionary will come in alphabetical order and that can be taken advantage of. You could implement something called a ‘binary search’ which is a pretty fast finding algorithm. The EdX CS50 course covers this concept in good detail.

On the other hand, we are javascript developers so we have built in methods that make things easier. For example, maybe you could have all the words ready to go in an array. In this case you could find it by simply writing:

words.indexOf(word) !== -1

edit: I still feel like loading so much data into your static files is probably not the best idea. This API looks promising:

1 Like

This looks like an excellent api for the job! Thanks for sharing!