I am working on a script to evaluate user response by comparing two arrays. (This is a survey to understand how well they know the information to the word.) I already have the code that I need, for example, to make the user's response lowercase and break it. All I need to do is find the number of differences / errors. For instance:
var correctanswer = ["The","quick","brown","fox","jumped","over","the","lazy","dog"]; var useranswer = ["The","brown","fox","jumped","up","and","over","the","really","lazy","cat"]; alert(counterrors(correctanswer, useranswer));
In this particular example, executing the function I'm looking for will return that the user made 5 errors (they are omitted βfastβ, added βupβ, βandβ and βreallyβ, and βdogβ is changed to βcatβ). As you can see, two arrays can have different lengths.
Does anyone know how to approach this? I thought this would probably be a loop:
for (x in correctanswer) { // compare correctanswer[x] to useranswer[x]... not sure how exactly. Seems tricky... }
Thanks for watching this! I saw the John Resig diff solution ( http://ejohn.org/projects/javascript-diff-algorithm/ ) and other similar things, and even a few comparisons with arrays, but nothing worked, as I found, return all the differences, whereas I want to see how many differences there are. Thanks again for your attention and please let me know about any questions.
Update: Thank you so much Magnar for the answer! It worked great.