title: Javascript - Check if an array contains only specified values
body: <p>How can I optimize a function that checks if an array contains only specified values not using hardcoded values?</p>
  
  <p>Here is the function</p>
  
  <pre><code>function containOnly(value1, value2, array){
    var result;
  
    for(i = 0; i &lt; array.length; i++){
      if(array[i] != value1 &amp;&amp; array[i] != value2){
        result = 0;
        break;
      } else
        result = 1;
    }
  
    if(result === 0)
      return false;
    else
      return true;
  }
  
  console.log(containOnly(1, 2, [2,1,2]));
  </code></pre>
  
  <p>This function will return true if an array contains specified values. In this function I use if statement to compare two values but how can I use an array of values instead of variables if I want to use more than two values? For example:</p>
  
  <pre><code>console.log(containOnly([1, 2, 3], [2,1,2,3,5]));
  </code></pre>