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 \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site