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

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

              
                <!-- Inspired by The Web Dev Bootcamp on Udemy - The RGB Color Game -->
<div class="container">
  <div class="game">
    <div class="top-section">
      <h1>Guess this HEX <br/><span id="colorDisplay"></span></h1>
      <div id="stripe">
        <button id="reset">New Colors</button>
        <span id="message"></span>
        <button class="mode">Easy</button>
        <button class="mode">Medium</button>
        <button class="mode selected">Hard</button>
      </div>
    </div>

    <div class="container">
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
        <div class="circle"></div>
    </div>
  </div>
</div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Lato);

body {
  background-color: #232323;
  margin: 0;
  font-family: "Lato", "Helvetica";
}

h1 {
  width: 500px;
  text-align: center;
  color: white;
  background-color: #F9690E;
  margin: 0 auto;
  font-size: 2.3em;
  font-weight: bold;
  text-transform: uppercase;
  line-height: 1.4;
  padding: 20px 0;
  letter-spacing: 7px;
}

.container {
  width: 500px;
  margin: 6px auto;
}

.circle {
  background-color: purple;
  width: 30%;
  padding-bottom: 30%;
  float: left;
  margin: 1.5%;
  border-radius: 50%;
  transition: background 0.8s;
  -webkit-transition: background 0.8s;
  -moz-transition: background 0.8s;
}

#colorDisplay {
  font-size: 130%;
  border: 1px solid white;
  border-radius: 5px;
}

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

#stripe {
  background-color: #fff;
  width: 500px;
  height: 50px;
  text-align: center;
  margin: 0 auto;
  padding: 0;
}

.selected {
  color: white;
  background: #F9690E;
}

button {
  border: none;
  background: none;
  text-transform: uppercase;
  font-family: "Lato", "Helvetica";
  height: 100%;
  font-weight: 700;
  letter-spacing: 3.5px;
  color: #F9690E;
  font-size: inherit;
  transition: all .5s;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  outline: none;
}

button:hover {
  color: white;
  background-color: #F9690E;
  color: white;
}
              
            
!

JS

              
                var numCircles       = 9,
    colors           = [],
    hexDigit         = ["a","b","c","d","e","f",0,1,2,3,4,5,6,7,8,9],
    pickedColor,
    circles          = document.querySelectorAll(".circle"), 
    colorDisplay     = document.getElementById("colorDisplay"),
    messageDisplay   = document.querySelector("#message"),
    h1               = document.querySelector("h1"),
    resetButton      = document.querySelector("#reset"),
    modeButtons      = document.querySelectorAll(".mode");

initiate();

function initiate (){
  setupModeButton();
  setupCircles();
  reset();
}

function setupModeButton (){
  for (var i = 0; i < modeButtons.length; i++){
    modeButtons[i].addEventListener("click", function (){
      modeButtons[0].classList.remove("selected");
      modeButtons[1].classList.remove("selected");
      modeButtons[2].classList.remove("selected");
      this.classList.add("selected");
      if(this.textContent === "Easy") {
        numCircles = 3;
      } else if (this.textContent === "Medium"){
        numCircles = 6;
      } else {
        numCircles = 9;
      }
      reset ();
    });
  }
}

function reset (){
  colors = generateRandomColors(numCircles);
  //pick a new random color from array
  pickedColor = pickRandomColor();
  //change colorDisplay to match pickedColor
  colorDisplay.textContent = pickedColor;
  resetButton.textContent = "New Colors";
  messageDisplay.textContent = "";
  //change colors of circles
  for (var i = 0; i < circles.length; i++){
    if (colors[i]){
      circles[i].style.display = "block";
      circles[i].style.background = colors[i];
    } else {
      circles[i].style.display = "none";
    }
  }
  //reset to default bg color
  h1.style.background = "#F9690E";
}

resetButton.addEventListener("click", function() {
  reset();
})


function setupCircles (){
  for (var i = 0; i < circles.length; i++) {
    // add click listeners to circles
    circles[i].addEventListener("click", function(){
        // store color of clicked circle
      var rgb = this.style.background;
      function rgb2hex(rgb) {
        if (rgb.search("rgb") == -1) {
          return rgb;
        } else {
          rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/);
          function hex(x) {
               return ("0" + parseInt(x).toString(16)).slice(-2);
            }
          return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); 
          }
        }
      var clickedColor = rgb2hex(rgb);
        // 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 = "#232323";
        messageDisplay.textContent = "Try Again";
      }
    });
  }
}

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

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

function generateRandomColors (num){
  var arr = [];
  for (var i = 0; i < num; i++) {
    arr.push(randomColor());
  }
  return arr;
}

function randomColor() {
  var hex1 = hexDigit[Math.floor(Math.random() * hexDigit.length)];
  var hex2 = hexDigit[Math.floor(Math.random() * hexDigit.length)];
  var hex3 = hexDigit[Math.floor(Math.random() * hexDigit.length)];
  var hex4 = hexDigit[Math.floor(Math.random() * hexDigit.length)];
  var hex5 = hexDigit[Math.floor(Math.random() * hexDigit.length)];
  var hex6 = hexDigit[Math.floor(Math.random() * hexDigit.length)];
  return "#" + hex1 + hex2 + hex3 + hex4 + hex5 + hex6;
}
              
            
!
999px

Console