cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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>
<head>
	<title>Color Game</title>
	<link rel="stylesheet" type="text/css" href="colorGame.css">
</head>
<body>
<h1>
	The Great 
	<br>
	<span id="colorDisplay">RGB</span> 
	<br>
	Color Game
</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>
	<script type="text/javascript" src="script.js"></script>
</body>
</html>
            
          
!
            
              body{
	background-color: #232323;
	margin: 0;
	font-family: "Montserrat", "Avenir";

}

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

}

.square{
	width: 30%;
	background: purple;
	padding-bottom: 30%;
	float: left;
	margin: 1.66%;
	border-radius: 15%;
	transition: background 1.0s;
	transition: all 0.3s;
	--webkit-transition:background 1.0s;
	--moz-transition:background 1.0s;
}

h1{
	color: white;
	line-height: 1.1;
	padding: 20px 0;
	text-align: center;
	background-color: steelblue;
	margin: 0;
	font-weight: normal;
	text-transform: uppercase;	
}

#colorDisplay{
	font-size: 200%;
}

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

}

.selected{
	color: white;
	background-color: steelblue
}

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

button:hover{
	color:white;
	background-color: steelblue;
}
#message{
	display:inline-block;
	width: 20%;
}


            
          
!
            
              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 modeButtons = document.querySelector(".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");
		if(this.textContent === "Easy"){
			numSquares = 3;
		} else {
			numSquares = 6;
		}
		reset();
		//figure out how many suqres to show
		//pick new colors
		//pick a new picked color
		//updatepage to reflect changes

	});
}

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

resetButton.addEventListener("click", function(){
	//generate all new colors
	colors = generateRandomColors(numSquares);
	//pick new random color from array
	pickedColor = pickColor();
	//change color Display to match picked color
	colorDisplay.textContent = pickedColor;
	this.textContent = "New Colors";
	messageDisplay.textContent = "";
	//change color of squares on the page
	for(var i = 0; i < squares.length; i++){
		squares[i].style.backgroundColor = colors[i];
	}
	h1.style.backgroundColor = "steelblue";
});

colorDisplay.textContent = pickedColor;

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

	//add click listeners to squares
	squares[i].addEventListener("click", function(){
		//grab color of clicked square
		var clickedColor = this.style.backgroundColor;
		//compare color to pickedColor
		if(clickedColor === pickedColor){
			messageDisplay.textContent = "Correct";
			changeColors(clickedColor);
			h1.style.backgroundColor = clickedColor;
			resetButton.textContent = "Play again?";
		}else{
			this.style.backgroundColor = "#232323";
			messageDisplay.textContent = "Try Again"

		}
	})
};

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

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

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;
};

function randomColor(){
	//make a "red" from 0 - 255
	var r = Math.floor(Math.random() * 256);
	//make a "green" from 0 - 255
	var g = Math.floor(Math.random() * 256);
	//make 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.
Loading ..................

Console