Here is the algorithm I've come up with. It works pretty well, but I wrote it from scratch. I'm just wondering if there is a better method out there to use.
Code:// w1 = The prompt string // w2 = The typed string // function checkAccuracy(w1,w2):Number{ var i=0; //pointer for w1 var j=0; //pointer for w2 var err=0; //number of errors found var offset=w2.length-w1.length; //the difference in length of w1 & w2. //Determines how many extra/missed characters to take into account (from the current position in the string) when processing. while(i<w1.length && j<w2.length){ var c1=w1.substr(i,1); //character in w1 at i var c2=w2.substr(j,1); //character in w2 at j if(c1==c2){ //if the two characters match //advance both string pointers i++; j++; }else if(w1.indexOf(c2,i)==-1){ //if c2 is not found past index i of w1 if(offset>0){ //if the typed string had any extra characters j++; //advance typed string pointer err++; //add to error count offset--; //decrease extra character count }else{ //otherwise, the current character is a wrong letter i++; //advance string pointers j++; err++; //add to error count } }else if(offset>=0){ //if c2 IS in w1... //if there are at least 0 extra characters j++; //advance w2 pointer err++; //add to error count offset--; //decrease extra character count }else if(offset<0){ //otherwise, if there were any missing characters i++; //advance to the next character in w1 err++; //add to error count offset++; //decrease missing character count (increase the offset toward 0) } } //return the number of errors found return err; }




Reply With Quote
