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

              
                <div class="container">
  <h1>Guess The Number!</h1>

  <p id="main"><em>Choose a number between 1 and 30</em></p>

  <form class="flex-form">
    <label for="num" class="form-el">Insert your number:</label>
    <input type="number" name="num" id="num" class="bigger-box" min="1" max="30" placeholder="Insert your number" required />
    <button type="submit" id="submit" class="btn bigger-box">Let's Play!</button>
    <button type="reset" id="reset" class="btn bigger-box">Play Again</button>
    <p id="show-num"></p>
    <p id="hint"></p>
    <p id="success"></p>
  </form>
</div>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Monoton&family=Roboto:ital,wght@0,700;1,400&display=swap");

:root {
  --heading: "Monoton", cursive;
  --smaller-font: "Roboto", sans-serif;

  --red: #dd2c00;
  --green: #76a21e;
  --yellow: #fcbf1e;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background-color: #1b1b2f;
  color: var(--yellow);
  font-family: var(--smaller-font);
  font-size: 1.5rem;
  margin: 0 auto;
  padding: 0 auto;
  max-width: 80%;
  height: 100%;
  text-align: center;
}

h1 {
  color: var(--red);
  font-family: var(--heading);
  font-weight: lighter;
  font-size: 3.5rem;
  margin-bottom: 1.5rem;
}

#main,
label {
  font-size: 1.2rem;
}

label {
  margin-bottom: 1em;
}

.container {
  position: absolute;
  top: 10%;
  bottom: 40%;
  left: 0;
  right: 0;
  align-items: center;
  max-width: 100%;
}

.flex-form {
  position: absolute;
  left: 0;
  right: 0;
  top: 12em;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.bigger-box {
  background-color: #fdfdc4;
  font-family: var(--smaller-font);
  font-size: 1rem;
  width: 15rem;
  height: 3rem;
  margin-bottom: 2rem;
  text-align: center;
  border: none;
  border-radius: 10px;
  outline: none;
  max-width: 85vw;
}

#hint {
  font-size: 2rem;
  font-style: italic;
  position: absolute;
  top: 12rem;
}

.message {
  font-size: 2rem;
  font-weight: 700;
}

#success {
  font-size: 3.2rem;
  position: absolute;
  top: 8rem;
}

.bigger-font {
  color: #f4f6ff;
  font-size: 2em;
  text-shadow: 2.5px 2.5px 5px var(--green);
}

#show-num {
  color: var(--green);
  font-family: var(--heading);
  font-size: 13rem;
  opacity: 0.7;
  position: absolute;
  top: -7.5rem;
  z-index: -100;
}

.btn {
  background-color: var(--yellow);
  cursor: pointer;
  font-weight: 700;
}

.btn:hover {
  background-color: var(--red);
  color: #ffffff;
  font-weight: bold;
}

#reset {
  margin-top: 8rem;
}

@media all and (max-width: 718px) {
  h1 {
    font-size: 2rem;
    margin-bottom: 1rem;
  }

  .container {
    top: 5%;
  }

  .flex-form {
    top: 20rem;
  }

  #hint {
    font-size: 1.5rem;
  }

  #success {
    font-size: 2rem;
    top: 6rem;
  }

  #show-num {
    font-size: 10.5rem;
    top: -8rem;
  }

  #reset {
    margin-top: 5rem;
  }
}

              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", () => {
  // grab elements & store in variable
  const main = document.querySelector("#main");
  const form = document.querySelector("form");
  const label = document.querySelector("label");
  const num = document.querySelector("#num");
  const success = document.querySelector("#success");
  const hint = document.querySelector("#hint");
  const showNum = document.querySelector("#show-num");
  const submit = document.querySelector("#submit");
  const reset = document.querySelector("#reset");

  //   initial setup
  
  let generateRandomNum = Math.floor(Math.random() * 30) + 1;
  console.log(`INIT: ${generateRandomNum}`);
  let tries = 1;
  // label.style.visibility = "visible";
  reset.style.visibility = "hidden";

  //   game start
  form.addEventListener("submit", (e) => {
    e.preventDefault();
    
    // get input value
    const getNum = e.target.num.value;

    // guessed number less than mystery number
    if (getNum < generateRandomNum) {
      tries++;
      // success.textContent = "";
      num.value = "";
      hint.textContent = "Oops... Maybe higher number?";
      submit.innerText = "Try Again";
      // guessed number greater than mystery number
    } else if (getNum > generateRandomNum) {
      tries++;
      // success.textContent = "";
      num.value = "";
      hint.textContent = "Hmmm... Let's try lower number";
      submit.innerText = "Try Again";
      // success guessed mystery number
    } else {
      main.innerHTML =
        '<span class="message">Yes! The mystery number is:</span>';
      success.innerHTML = `You got me after <span class="bigger-font">${tries}</span> times!`;
      hint.textContent = "";
      showNum.textContent = `${getNum}`;
      reset.style.visibility = "visible";
      label.style.visibility = "hidden";
      num.style.visibility = "hidden";
      submit.style.visibility = "hidden";
    }
    console.log(`TRY: ${tries}`);
  });

  //   reset game
  reset.addEventListener("click", (e) => {
    e.preventDefault();

    num.value = "";
    reset.style.visibility = "hidden";
    label.style.visibility = "visible";
    num.style.visibility = "visible";
    submit.style.visibility = "visible";
    success.textContent = "";
    showNum.textContent = "";
    submit.textContent = "Let's play!";
    main.textContent = "Choose a number between 1 and 30";

    generateRandomNum = Math.floor(Math.random() * 30) + 1;
    tries = 1;

    console.log(`from reset: ${generateRandomNum}`);
    console.log(`tries from reset: ${tries}`);
  });
});

              
            
!
999px

Console