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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <div id="intro">
  <p>
    Think of a number between <span id="min">MIN</span> and <span id="max">MAX</span>.
    <br> I will be able to guess in <span id="dif">N</span> tries or less.
  </p>
  <p>
    <button id="ok">OK?</button>
  </p>
</div>

<div id="game">
  <p>Is your number <span id="guess">N</span>?</p>
  <p>
    <button id="yes">Yes</button> or No, it's
    <button id="lower">Lower</button> or
    <button id="higher">Higher</button>.
  </p>
</div>

<div id="victory">
  <p>I got it in only <span id="tries">N</span> tries!</p>
  
  <p>
    <button id="reset">Again?</button>
  </p>
</div>
            
          
!

CSS

            
              @import 'https://fonts.googleapis.com/css?family=Alegreya+Sans';

body,
html {
  height: 100%;
}

body {
  font-family: 'Alegreya Sans', sans-serif;
  font-size: 24pt;
  background-color: #FEECA4;
  color: #0FA6BE;
  display: flex;
  align-items: center;
  justify-content: center;
}

span {
  font-weight: bold;
  color: #A00C75;
}

button {
  font-family: 'Alegreya Sans', sans-serif;
  font-size: 1em;
  border: none;
  background-color: #A00C75;
  color: #D3E318;
}

button:hover {
  background-color: #352549;
}

button:active {
  background-color: #352549;
  color: #A00C75;
}

button:focus {
  outline: 0;
}

#game {
  display: none;
}

#victory {
  display: none;
}

#game, #victory, #intro {
  width: 18em;
}
            
          
!

JS

            
              const MIN = 1
const MAX = 1000000

let upper, lower, guess, tries

const $ = (s) => document.querySelector(s)

const main = () => {
  $('#min').textContent = MIN
  $('#max').textContent = MAX
  $('#dif').textContent = Math.round(Math.log(MAX) / Math.log(2))
  $('#ok').addEventListener('click', startGame)
  $('#higher').addEventListener('click', guessHigher)
  $('#lower').addEventListener('click', guessLower)
  $('#yes').addEventListener('click', guessCorrect)
  $('#reset').addEventListener('click', startGame)
}

const startGame = () => {
  $('#intro').style.display = 'none'
  $('#game').style.display = 'block'
  $('#victory').style.display = 'none'
  tries = 0
  lower = MIN
  upper = MAX
  updateGame()
}

const guessHigher = () => {
  lower = guess + 1
  updateGame()
}

const guessLower = () => {
  upper = guess - 1
  updateGame()
}

const updateGame = () => {
  guess = Math.floor((lower + upper) / 2)
  $('#guess').textContent = guess
  tries++
}

const guessCorrect = () => {
  $('#game').style.display = 'none'
  $('#victory').style.display = 'block'
  $('#tries').textContent = tries
}

main()
            
          
!
999px

Console