Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
	<title>Color Game</title>
	<link rel="stylesheet" type="text/css" href="colorGame.css">
</head>
<body>
	<!--title here and the squares,Begins here-->
	<h1>The Great
		<br>
		<span id="colorDisplay"> RBG</span>
		<br>
		 Color Game
		</h1>

	<div id="stripe">
		<button id="reset">New Colors</button>
		<span id="message"></span>
		<button id="easyBtn">Easy Mode</button>
		<button id="hardBtn" class="selected">Hard Mode</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>
	<!--ends here-->
	<script type="text/javascript" src="colorGame.js"></script>
</body>
</html>
              
            
!

CSS

              
                body {
	background-color: grey;
	margin: 0;
	font-family: "Avenir", "Monsterrat";
}

.square {
	width: 30%;
	background: grey;
	padding-bottom: 30%;
	float: left;
	margin: 1.66%;
	border-radius: 15%;
	transition: background 1.2s;
	-webkit-transition: background 0.7s;
	-moz-transition: background 0.7s;
}
.square:hover {
  box-shadow: 0px 5px 5px 0px;
  transition: box-shadow 0.6s ease-out;
}


#container {
	margin: 20px auto;
	max-width: 600px;
}

h1 {
	text-align: center;
	line-height:  1.1;
	color: white;
	background: steelblue;
	margin: 0;
	text-transform: uppercase;
	padding:  20px 0;
}
#colorDislay {
	font-size: 200%;
}
span {
	color: white;
}

#message {
	display:  inline-block;
	width: 20%;
}

#stripe {
	background: white;
	height:  30px;
	text-align: center;
	color:black;
}

.selected {
	background: navy;
}

button {
	border: none;
	background: none;
	text-transform: uppercase;
	height:  100%;
	font-weight: 700;
	color: steelblue;
	letter-spacing: 1px;
	font-size: inherit;
	transition: all 0.5s;
	outline: none;

}
button:hover {
	color: white;
	background: steelblue;
}	
              
            
!

JS

              
                
var numSquares = 6;
var colors = generateRandomColors(numSquares);
var squares = document.querySelectorAll(".square");
var pickedColor = pickColor();
var colorDisplay = document.getElementById("colorDisplay");
var messageDisplay = document.querySelector("#message");
var h1 = document.querySelector("h1");
var resetButton = document.querySelector("#reset");
var easyBtn = document.querySelector("#easyBtn");
var hardBtn = document.querySelector("#hardBtn");
//asi es como cambia el clicked
easyBtn.addEventListener("click", function(){
  hardBtn.classList.remove("selected")
  easyBtn.classList.add("selected");
  numSquares = 3;
  colors = generateRandomColors(numSquares);
  pickedColor = pickColor();
  colorDisplay.textContent = pickedColor;
  for(var i = 0; i < squares.length; i++){
    if(colors[i]){
      squares[i].style.background = colors[i];
    } else {
      squares[i].style.display = "none";
    }
  }
});

hardBtn.addEventListener("click", function(){
  hardBtn.classList.add("selected")
  easyBtn.classList.remove("selected");
  numSquares = 6;
  colors = generateRandomColors(numSquares);
  pickedColor = pickColor();
  colorDisplay.textContent = pickedColor;
  for(var i = 0; i < squares.length; i++){
     squares[i].style.background = colors[i];
     squares[i].style.display = "block";
    
  }
	
});

resetButton.addEventListener("click", function (){
	//generate all the new colors
	colors = generateRandomColors(numSquares);
	// pick a new random color from array
	pickedColor = pickColor();
	//change color display to match picked color
	colorDisplay.textContent = pickedColor;
  this.textContent = "New Colors";

  messageDisplay.textContent = "";
	//change colors of squares 
	for(var i = 0; i < squares.length; i++){
		squares[i].style.background = colors[i];
	}
	h1.style.background = "teal";
})

colorDisplay.textContent = pickedColor;

for(var i = 0; i < squares.length; i++) {
	//add initial colors to squares
  squares[i].style.background = colors[i];

  //add click listeners to squares
  squares[i].addEventListener("click", function (){
  	//grab color of clicked square
  	 var clickedColor = this.style.background;
  	 // compare color to pickedColor
  	 console.log(clickedColor, pickedColor);
  	 if(clickedColor === pickedColor) {
  	 	messageDisplay.textContent = "Correct!";
  	 	resetButton.textContent = "Play Again?"
  	 	changeColors(clickedColor);
  	 	h1.style.background = clickedColor;

  	 } else {
  	 	this.style.background = "teal";
  	 	messageDisplay.textContent = "Try Again"
  	 }
  });
}

function changeColors(color) {
	//loop through all squares 
	for(var i = 0; i < squares.length; i++) {
    // change each colo 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 a array
 	var arr = []
 	//add num random colors to array
 	for(var i = 0; i < num; i++) {
 		arr.push(randomColor())
 	// get random color and push into arr

 	}
 	// return that array
 	return arr;
 }

 function randomColor () {
 	// pick a red from 0-255
 	 var r = Math.floor(Math.random() * 256);

 	 var g = Math.floor(Math.random() * 256);

 	 var b = Math.floor(Math.random() * 256);
 	 return "rgb(" + r + ", " + g + ", " + b + ")";

 }
              
            
!
999px

Console