Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <h1 class="heading"> Jack's Awesome<br><span id="colorDisplay"></span><br> Color Game</h1>

<div id="stripe">
  <button id="reset">New Game</button>
  <span id="message"></span>
  <button class="mode">Easy</button>
  <button class="mode selected">Hard</button>
</div>

<div class="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>

<footer><h3>Built by <a href="http://www.jacklyons.me" href="target=_blank">Jack</h3></footer>
              
            
!

CSS

              
                body {
  background: #232323;
  text-align: center;
  color: white;
  font-family: "Ubuntu", "sans-serif";
  margin: 0;
}

/*
 * TEXT
 */
button {
  color: steelblue;
  font-weight: 700;
  text-transform: uppercase;
  outline: none;
  border: none;
  height: 30px;
  padding: 0 15px;
  background: #F4F7F6;
}

button:hover {
  background: steelblue;
  color: white;
  transition: .3s;
  -webkit-transition: .3s;
  -moz-transition: .3s;
}

button.selected {
  background: steelblue;
  color: white;
  transition: .3s;
  -webkit-transition: .3s;
  -moz-transition: .3s;
}

div#stripe {
  background: #F4F7F6;
  text-align: center;
  height: 100%;
  color: black;
}

h1.heading {
  margin: 0;
  padding: 10px;
  background: steelblue;

}

span#colorDisplay {
  color: white;
}

span#message {
  color: tomato;
  font-weight: 700;
  font-size: 1.2em;
  display: inline-block;
  text-transform: uppercase;
  width: 20%;
}


/*
 * SQUARES
 */
div.container {
  width: 600px;
  margin: 0 auto;
  padding-top: 20px;
}

div.square {
  width: 30%;
  margin: 1.66%;
  padding-bottom: 30%;
  float: left;
  border-radius: 25px;
  cursor: pointer;
}
              
            
!

JS

              
                /*
 * SET VARIABLES
 */
var numSquares = 6,
    colors     = [],
    pickedColor,
    h1             = document.querySelector("h1"),
    squares        = document.querySelectorAll(".square"),
    colorDisplay   = document.getElementById("colorDisplay"),
    messageDisplay = document.querySelector("#message"),
    resetButton    = document.querySelector("#reset"),
    modeButtons    = document.querySelectorAll(".mode");

init();
/*
 * Initialize Code
 */
function init(){
	setupModeButtons();
	setupSquares();
	reset();
}
/*
 * PROGRAM BUTTONS
 */
// Add a selected class to the easy and hard buttons
function setupModeButtons(){
	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();
		});
	}
}
/*
 * GAME FUNCTIONALITY
 */
function setupSquares() {
	for(var i = 0; i < squares.length; 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
			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";
			}
		});
	}
}
/*
 * This function resets the game
 */
function reset() {
  // Colors returns an array of random colors
	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";
}

resetButton.addEventListener("click", function() {
	reset();
});
/*
 * This function changes all the squares to the winning color
 */
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;
	}
}
/*
 * This function picks a random number as the index of the six squares
 */
function pickColor() {
  // colors array has already been filled with random colors
	var random = Math.floor(Math.random() * colors.length);
  // a random color from the array is picked
	return colors[random];
}
/*
 * This function creates a random RGB color
 */
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 + ")";
}
/*
 * This function creates an array of random colors using num to specify the amount of colors to be generated
 */
function generateRandomColors(num) {
	// Make an array
	var arr = [];
	// Repeat num times
	for (var i = 0; i < num; i++) {
		// Get random color and push into arr
		arr.push(randomColor());
	}
	// Return that array
	return arr;
}

              
            
!
999px

Console