word decoder by javascript?

word decoder by javascript?

To start, try breaking down the problem into things youll need; think nouns and verbs. This is simply rewriting the problem in new terms. You need:

  • word: just a string, but its a noun youll need, so list it.
  • dictionary: a collection of words to choose from (during testing, you dont need many)
  • display: these become HTML elements, since youre working with JS
    • scrambled word
    • text input
    • submit button to check answer
    • score
    • wrong answer notifier
  • to scramble a word
  • to compare words: how can you compare two words to see if one is a permutation of the other? Do it right and anagrams arent a problem.
  • to check an answer
  • to increment score
  • to notify user of incorrect answer
  • to present a new scrambled word

Any item beginning with to is a verb; anything else is a noun. Nouns become objects, verbs become methods/functions.

The above is mostly a top-down approach, in contrast with bottom-up (note that top-down vs bottom-up isnt an either-or proposition). Other approaches that might help with not knowing where to start are test driven development or its offshoot, behavior driven development. With these you start by defining, in code, what the program should do, then fill in the details to make it do that.

A hint on comparing words: the problem is basically defining an equivalence class—two strings are equivalent if one is a permutation of the other. The permutations of a string, taken together, form the equivalence class for that string; two strings are in the same equivalence class if the strings are equivalent. As the linked document points out, equivalence classes are well represented by picking a single element of the class as the class representative. Lastly, you can turn the equivalence class definition around: two strings are permutations of each other if they are in the same equivalence class.

Look into loading a dictionary via XHR.
there are tons of those available online [http://www.mieliestronk.com/wordlist.html NOTE: it contains some swear words, if youre going to be doing this for academic purposes, since its your homework, you should look for a clean list]…

For scrambling the word: make your string into a char array, then find an array shuffle function [they are simple to write, I wrote one for my implementation of Bogosort]…

function shuffle(b)
{
    var a = b.concat([]); //makes a copy of B, b wont be changed...
    var final = [];
    while(a.length != 0)
    {
        //0 -> a length-1
        var targIndex = Math.round((a.length-1)*(Math.random()));
        var value = a[targIndex]
        a.remove(targIndex);
        final.push(value);
    }
    return final;
}

When the user is done inputting, simply compare input with the answer [case insensitive, ignore spaces] As stated in comments, there are also the possibility of anagrams, so be sure to check for those… perhaps, you could simply verify the word exists in the dictionary.

word decoder by javascript?

Leave a Reply

Your email address will not be published. Required fields are marked *