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.

            
              <h1>
	Guess the Color
  <br>
  <span id="colorDisplay">RGB</span>
</h1>

<div id="stripe">
	<button id="reset">New Colors</button>
	<span id="message"></span>
  <button class="mode">easy</button>
  <button class="mode selected">hard</button>
</div>

<div id="container">
	<div class="square"></div>
	<div class="square"></div>
	<div class="square"></div>
	<div class="square"></div>
	<div class="square"></div>
	<div class="square"></div>
</div>
            
          
!
            
              body{
  background-color: #232323;
  font-size: 16px;
  margin:0;
  font-family: "Lato";
  text-transform: uppercase;
}
h1{
  font-weight: normal;
  text-align: center;
  color: #fff;
  font-size: 1.5rem;
  margin: 0;
  line-height: 1.1;
  padding: 5% 0;
}
#colorDisplay{
  padding-top: 1%;
  font-size: 1.5rem;
}
#stripe{
  background: white;
  height: 30px;
  text-align: center;
  color: black;
}
.selected{
  background-color: steelblue;
  color: #fff;
}
#container{
  max-width: 600px;
  margin: 20px auto;
}
.square{
  border-radius: 15%;
  width: 30%;
  margin: 1.666%;
  padding-bottom: 30%;
  float: left;
  background: purple;
  transition: background .6s;
  -webkit-transition: background .6s;
  -moz-transition: background .6s;
}
#message{
  display: inline-block;
  width: 20%;
}
button{
  height: 100%;
  border:none;
  background: none;
  text-transform: uppercase;
  font-weight: 700;
  color: steelblue;
  letter-spacing: .5px;
  font-size: inherit;
  transition: all .333s;
  outline: none;
}
button:hover{
  color:#fff;
  background: steelblue;
}

@media screen and (max-width: 500px){
  #reset{
    float: left;
    padding-left: 2%;
  }
  button{
    font-size: .8rem;
  }
  .mode{
    float:right;
    padding-right: 2%;
  }
}
            
          
!
            
              var colors = [];
var numSquares = 6;
var pickedColor;
var colorDisplay = document.getElementById("colorDisplay");
var messageDisplay = document.querySelector("#message");
var h1 = document.querySelector("h1");
var resetButton = document.querySelector("#reset");
var easy = document.querySelector("#easyBtn");
var hard = document.querySelector("#hardBtn");
var modeButtons = document.querySelectorAll(".mode");
var squares = document.querySelectorAll(".square");

init();

function init(){
  setupButtons(); 
  setupSquares();
  reset();
}

function setupButtons(){
  //mode
  for(var i = 0; i < modeButtons.length; i++ ){
    modeButtons[i].addEventListener("click", function(){
      modeButtons[0].classList.remove("selected");
      modeButtons[1].classList.remove("selected");       
      this.classList.add("selected");
      this.textContent === "easy" ? numSquares = 3: numSquares = 6;
      reset();
    });
  }
  //reset
  resetButton.addEventListener("click", function() {
  reset();  
});
}  

function setupSquares(){
  for(var i = 0; i < squares.length; i++){
	  // add listeners to color squares
	  squares[i].addEventListener("click", function() {
		  var clickedColor = this.style.background;
		  //compare color to pickedColor
		  if(clickedColor === pickedColor) {
			  messageDisplay.textContent = "Correct!";
			  resetButton.textContent = "Play Again?";
			  changeColors(clickedColor);
			  h1.style.background = clickedColor;
		  }else{
			  this.style.background = "#232323";
			  messageDisplay.textContent = "Try Again";
		}
	});
}
}

function reset(){
	colors = generateRandomColors(numSquares);
  //pick a new random color from array
	pickedColor = pickColor();
  //change colorDisplay to match picked Color
	colorDisplay.textContent = pickedColor;
  resetButton.textContent = "New Colors";
  messageDisplay.textContent = "";
	//change colors of squares
	for(var i = 0; i < squares.length; i++) {
    if (colors[i]){
      squares[i].style.display = "block";
      squares[i].style.background = colors[i];
    }else{
      squares[i].style.display = "none";
    }
	}
	h1.style.background = "steelblue";
}

function changeColors(color) {
	//loop through all squares
	for(var i = 0; i < squares.length; i++) {
		//change each color to match given color
		squares[i].style.background = color;
	}
}

function pickColor() {
	var random = Math.floor(Math.random() * colors.length);
	return colors[random];
}

function generateRandomColors(num) {
	//make an array
	var arr = [];
	//add num random colors to arr
	for(var i = 0; i < num; i++) {
		//get random color and push into arr
		arr.push(randomColor());
	}
	return arr;
}

function randomColor() {
	//pick a "red" from 0 - 255
	var r = Math.floor(Math.random() * 256);
	//pick a "green" from 0 - 255
	var g = Math.floor(Math.random() * 256);
	//pick a "blue" from 0 - 255
	var b = Math.floor(Math.random() * 256);
	return "rgb(" + r + ", " + g + ", " + b + ")";
}

            
          
!
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