title: Function changing global variable
body: <p>I have this array that I pass into a function that's suppose to rotate the array 90 deg and return the result. I then want to check whether the new array is colliding with other things on the canvas.</p>
  
  <pre><code>let newArray = [
  [0,0,1],
  [1,1,1],   
  [0,0,0]
  ];
  
  let test = rotate(newArray);
  
  if ( collision(test) ){
    draw(newArray);
  } else {
    draw(test);
  }
  </code></pre>
  
  <p>Here's the problem. I think rotate() is changing the newArray-variable so it doesn't matter what the if-statement evaluate to, the draw() function will always do the same thing.</p>
  
  <p>I want to select the array to draw after checking if the rotated matrix collides with other stuff.</p>
  
  <pre><code>function rotate(matrix) {
  
  matrix = matrix.reverse();
  
  for (var i = 0; i &lt; matrix.length; i++) {
   for (var j = 0; j &lt; i; j++) {
    var temp = matrix[i][j];
    matrix[i][j] = matrix[j][i];
    matrix[j][i] = temp;
   }
  }
  return matrix
  }
  
  
  function collision(mat){
  
  let collision = false;
  
  for ( let i = 0; i &lt; mat.length; i++){
  for ( let j = 0; j &lt; mat.length; j++){
    if (mat[i][j] == 1){
      let test = ctx.getImageData(x + 10*j +10, y + 10*i, 1, 1);
        if (test.data != "255,255,255,255" &amp;&amp; test.data != "0,0,0,0"){
          collision = true;
        }
    }
   }
   }
  return collision;
  }
  </code></pre>