Wildcards

Example

Project: Allow the user to input letters with blanks, and output the words that can be made by filling in the blanks.

Technique

Break down the input into smaller inputs until you are left with one letter. Once again, you can use recursion. Recursive functions always have a base case and a recursive case. The base case returns a result, and the recursive case reduces the problem toward the base case. It is very important that you have a base case, otherwise, the function will never terminate.

In this case, the function will take a string as a parameter, extract one letter, and then call itself with the remaining letters. Eventually, the function will stop calling itself when it cannot extract any more letters.

Hints

For cycling through characters, refer to the character codes page.

For breaking down strings into smaller strings, use the substring method.

Word Dictionary

Download a list of words here.

Fetch the list of words and save it to a dictionary variable:
// create an empty dictionary variable
var dictionary = {};

// fetch the list of words
var request = new XMLHttpRequest();
request.open("GET", "dictionary.txt");
request.addEventListener("load", createDictionary);
request.send();

// this function gets called after fetching the list of words
function createDictionary() {
    // get all of the words in the file and save it to an array
    var words = this.response.split("\n");

    // loop through the array of words
    for (var i = 0; i < words.length; i++) {
        // save each word to the dictionary variable
        dictionary[words[i]] = true;
    }
}
Last topic Next topic