Hey, for the same Japanese project as before, some Kanji have multiple accepted meanings, so I am expressing meaning as an array. How can I check that Student Input matches ANY element of an array? What I currently have only checks the first meaning. Also, how can I make this not be case-sensitive, or is it already?
function checkAll () {
for (var currentKanji in allKanji) {
if (currentKanji.studentInput == currentKanji.meaning[0]){
total += 1;
}
total = total/160;
}
}
I would use a List. instead of an Array, and check
if (meaningList.Contains(studentInput)){
Or you could create another for loop, and do
for (var i: int = 0; i< currentKanji.meaning.Length; i++)
{
if (currentKanji.studentInput == currentKanji.meaning*)*
total += 1;
}
Try this:
import System.Linq;
import System.Collections.Generic;
...
if( currentKanji.meaning.Any(function(m){return m==currentKanji.studentInput;})) {
total++;
}
Or if you want to be really short, you can skip the outer loop too (although I’m assuming you wanted that total to be divided by 160 in a slightly different place, it didn’t make sense to me where it was):
total = allKanji.Aggregate(0, function(c,n) { return c + ( n.meaning.Any( function(m) { return m==n.studentInput; } ) ? 1 : 0 ); }) / 160;