For example suppose we call matchOne as follows matchOne cat
For example, suppose we call matchOne() as follows.
matchOne( [“cat”, “dog”])
if the user when prompted for a string, enters \"catfish\", the function raises the alert box
Hint: Where str1 and str2 are strings,
Str1.indexOf( str2 )
Solution
ar = [\"cat\",\"dog\"];
function matchOne(ar){
while(true){
// get user input
str = prompt(\"Enter a string,Cancel to exit.\");
if(str===null){
break;
}
else{
// check substring.
for(i=0;i<ar.length;i++){
if(str.indexOf(ar[i])!=-1){
alert(ar[i]+\" occurs in \"+str);
break;
}
}
}
}
}
matchOne(ar);
![For example, suppose we call matchOne() as follows. matchOne( [“cat”, “dog”]) if the user when prompted for a string, enters \ For example, suppose we call matchOne() as follows. matchOne( [“cat”, “dog”]) if the user when prompted for a string, enters \](/WebImages/9/for-example-suppose-we-call-matchone-as-follows-matchone-cat-1000848-1761515544-0.webp)