Posted by: fuckeetow October 10, 2011
javascript array
Login in to Rate this Post:     1       ?         Liked by

if(array[i].toString()=="ij" && array[i].toString()=="kl")
The conditional statement above is never going to pass.
What your doing here is that you are comparing the same element of array i.e. array[i] to both "ij" and "kl" and excepting it to be equal to both of them at the same time using the operator &&.

Also, since Javascript is case-sensitive, the array declaration should look as follows -

var array = new Array();

But, that's a deprecated way of declaring array.
The new and better way is by using Javascript array literals as follows -

var array = [ ];

And the solution would be something like this -

// if we an array as follows -
var array = ['ab', 'cd', 'ef', 'gh', ..., 'wx', 'yz'];  // also assuming that array[9] = 'yz'

// we want to see if it contains both 'ij' and 'kl' so
// we have to look for both of them separate as follows

var matchCount = 0;

// also since you have said that you want to return 'xy' or array[9], lets use function becuase
// the only place from where you can return (a value) is a function
function contains() {
     for(var indx=0; indx<array.length(); indx++) {
          if(array[indx] == 'ij') {
               matchCount++;
          }

          if(array[indx] == 'kl') {
               matchCount++;
          }
     }

     if(matchCount == 2) {  // if it contains both 'ij' and 'kl' then
          return array[9]; // return array[9] (which is 'yz')
      }

      // else you can just return undefined
     return undefined;
}
Read Full Discussion Thread for this article