title: Inefficient JavaScript Algorithm, How do I make the following code more efficient?
body: <p><strong>Problem Statement:</strong> Given two arrays <strong>a</strong> and <strong>b</strong> write a function comp(<strong>a, b</strong>) (compSame(<strong>a, b</strong>) in Clojure) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in <strong>b</strong> are the elements in <strong>a</strong> squared, regardless of the order.</p>
  
  <p><strong>My Solution:</strong> </p>
  
  <pre><code>function comp(array1, array2){
  
    var test;
  
    if( array1.length &lt;= 0 || array2.length &lt;= 0 || array1 == null || array2 == null || array2.length != array1.length || array1==undefined || array2==undefined){
      return false;
    }else{
      for(i=0; i&lt;array2.length; i++){
        for(j=0; j&lt;array1.length; j++){
          if(array2[i] === Math.pow(array1[j],2)){
            test = true;
            break;
          }else{
            test = false;
          }
        }
        if(test==false)
          return false;
      }
  
      if(test === true)
        return true;
      else
        return false;
    }
  }
  </code></pre>
  
  <p>Also any constructive criticism about the code is appreciated as I'm still just a wookie so always looking to improve.</p>
  
  <p><strong>Test Example:</strong> </p>
  
  <pre><code>var a1 = [80, 2, 65, 68];
  var a2 = [6401, 4, 4225, 4624];
  </code></pre>
  
  <p>For the end result all I want is for true to be returned if true and likewise for false. The program solves all the test cases however it times out.</p>