Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
                 <div>
        <button onClick="start()">START</button>
        <button onClick="reset()">RESET</button>
      </div>
      <br>
      <div id="boxes">
        <canvas id="greenC" width="200" height="100" style="border:1px solid #000000" onClick="clickBox(this)">
      </canvas>
        <canvas id="yellowC" width="200" height="100" style="border:1px solid #000000" onClick="clickBox(this)">
      </canvas>
        <br>
        <canvas id="redC" width="200" height="100" style="border:1px solid #000000" onClick="clickBox(this)">
      </canvas>
        <canvas id="blueC" width="200" height="100" style="border:1px solid #000000" onClick="clickBox(this)">
      </canvas>
      </div>
      <h1 id="x">combinations =</h1>
      <h1 id="z">guesses =</h1>
      <h1 id="timez">round = </h1>
            
          
!
            
              
#greenC {
    background-color:#2CA6A4;
  }
#yellowC {
    background-color:yellow;
  }
#redC {
    background-color:#BA1B1D;
  }
#blueC {
    background-color:blue;
  }
            
          
!
            
              var greenBox = document.getElementById("greenC");
var yellowBox = document.getElementById("yellowC");
var redBox = document.getElementById("redC");
var blueBox = document.getElementById("blueC");
var combinations = [];
var guesses = [];
var actual = [];
var len;
var ss;

//suoni
var greenSound = new Audio("http://www.noiseaddicts.com/samples_1w72b820/3720.mp3");
var yellowSound = new Audio("http://www.noiseaddicts.com/samples_1w72b820/3739.mp3");
var redSound = new Audio("http://www.noiseaddicts.com/samples_1w72b820/3723.mp3");
var blueSound = new Audio("http://www.noiseaddicts.com/samples_1w72b820/3732.mp3");
//arrays
var boxes = [greenBox,yellowBox,redBox,blueBox];
var sounds = [greenSound,yellowSound,redSound,blueSound];
var counter = 0;
var round = 0;


//functions
var pushRandom = function(){
  //  make a box sound and glow and call backtonorm()
  combinations.push(Math.floor(Math.random()*4));
  boxes[combinations[combinations.length - 1]].style.border="3px solid black";
  sounds[combinations[combinations.length - 1]].play();
  setTimeout(function(){backNormal(boxes[combinations[combinations.length - 1]])},1000);
  document.getElementById("x").textContent = "combinations = " + combinations;
};

var start = function(){
  counter++;
  round++;
  
  if (combinations.length < counter){
    pushRandom();
  }

  document.getElementById("x").textContent = "combinations = " + combinations;
  document.getElementById("timez").textContent = "round = " + round;
  console.log("combinations = " + combinations);
  console.log("guesses = " + guesses);
  console.log("counter = " + counter);
};

var backNormal = function(x){
  x.style.border="1px solid black";
};

var reset =function(){
  combinations = [];
  guesses = [];
  document.getElementById("x").textContent = "combinations = " + combinations;
  document.getElementById("z").textContent = "guesses = " + guesses;
};

var clickBox = function(box){
  
   if(box.id === "greenC"){
     guesses.push(0);
     greenSound.play();
     greenBox.style.border ="3px solid black";
     setTimeout(function(){backNormal(greenBox)},1300);

   }else if(box.id === "yellowC"){
     guesses.push(1);
     yellowSound.play();
     yellowBox.style.border="3px solid black";
     setTimeout(function(){backNormal(yellowBox)},1300);
   }else if(box.id === "redC"){
     guesses.push(2);
     redSound.play();
     redBox.style.border="3px solid black";
     setTimeout(function(){backNormal(redBox)},1300);
   }else if(box.id === "blueC"){
     guesses.push(3);
     blueSound.play();
     blueBox.style.border="3px solid black";
     setTimeout(function(){backNormal(blueBox)},1300);
   }
  document.getElementById("z").textContent = "guesses = " + guesses;

  if(checkArs(combinations,guesses)){
    for(var i = 0;i<combinations.length;i++){
      actual.push(combinations[i]);
    }
    console.log("actual is now " + actual);
    combinations = [];
     guesses = [];
     document.getElementById("x").textContent = "combinations = " + combinations;
     document.getElementById("z").textContent = "guesses = " + guesses;
    prova();    
};
}

var prova = function(){
  len = actual.length;
    if(len > 0){
      ss = actual.shift();
      console.log("tolgo da actual = " + ss);
      setTimeout(function(){
        boxes[ss].style.border="3px solid black";
        setTimeout(function(){backNormal(boxes[ss])},900);
        sounds[ss].play()
        console.log("playing " + ss);
        combinations.push(ss);
        document.getElementById("x").textContent = "combinations = " + combinations;
        document.getElementById("z").textContent = "guesses = " + guesses;
      },2100);  
      setTimeout(prova,2200);
    }else if(len ===0){
      setTimeout(function(){pushRandom()},2000);
      document.getElementById("x").textContent = "combinations = " + combinations;
      document.getElementById("z").textContent = "guesses = " + guesses;
      return
    }
    
};


var checkArs = function(arr1,arr2){
  for(var i = arr1.length; i--;){
    if(arr1[i] !== arr2[i]){
      return false;
    }else {
      return true;
    }
  }
}




            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console