jeudi 21 mai 2015

Function to check presence of substrings

I have a really long list of if statements using this format. I can't figure out a better way to get detect the two substrings, but I know there is one.

The data is a string (StTResult) that will look something like: "Could you turn the music on?". The arrays are the keywords it searches for within the StTResult string, so "categories[0]" for instance is "music" and "musicParameter[5]" is "on". So basicly the if statement checks wether the words "on" and "music" are in the string StTResult.

Ideally I would like to replace this with some kind of function that so that I can just call the function with the two parameters and get a result in a true or false form. How can I best improve this if statement, or replace it with a function?

Thanks in advance!

function getCommandCategory() {

if (StTResult.indexOf(categories[0]) >= 1 && StTResult.indexOf(musicParameter[5]) >= 1) {
openURL();
} 
else if (StTResult.indexOf(categories[0]) >= 1 && StTResult.indexOf(musicParameter[9]) >= 1) {
openURL2();
}
};

Aucun commentaire:

Enregistrer un commentaire