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>Is it Fizz or Buzz?</h1>
<p>
  Type in a number and find out. 
  <br><em>Try 3, 5, or 15 or something else</em> 
  <br>👇
</p>

<form>
  <label for="number"></label>
  <input id="number" type="text">
  <button type="submit" id="the-button">What is it?</button>
</form>

<h2>It's <span id="result">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> !</h2>

<small>
  <h3>What is FizzBuzz?</h3>
    FizzBuzz is a very simple programming task, used in software developer job interviews, to determine whether the job candidate can actually write code. It was invented by Imran Ghory, and popularized by Jeff Atwood. Here is a description of the task: <em>Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.</em>
  <br><br>
  <b>Open the console for a 1-100 version, common in interview questions.</b>
</small>
              
            
!

CSS

              
                /* TODO: Small animation when "result" box updates */

body {
  font-family: system-ui, sans-serif;
  text-align: center;
  margin: 2em;
  display: grid;
  justify-content: center;
  align-items: center;
}

@media (max-width: 750px) {
  body {
    font-size: 12px;
  }
}

h1 {
  font-size: 5em;
  margin-bottom: 0;
}

h2 {
  width: max-content;
  margin: 5rem auto;
  padding: 2rem 4rem;
  font-size: 5em;
  color: #fff;
  background-color: #000;
  transform: rotate(-2deg);
  border-radius: .25em;
}

p {
  font-size: 1.5em;
  line-height: 1.5;
}

form {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 2em;
}

input {
  border: solid 4px #000;
  border-radius: .5em;
  padding: .5rem 1rem;
  margin-right: .5em;
  max-width: 4em;
  font-size: 2em;
  text-align: center;
}

input:focus {
  outline: none;
  border: solid 5px #000;
}

button {
  border: solid 4px #000;
  border-radius: .5em;
  color: #fff;
  background-color: #000;
  font-weight: bold;
  font-size: 1.1em;
  padding: 1rem 2rem;
}

#result {
  text-decoration: underline;
  cursor: pointer;
}

small {
  display: block; 
  width: 70%; 
  margin: 0 auto;
  line-height: 1.8;
}

              
            
!

JS

              
                /**
 * @param {number} n
 * @return {string[]}
 */

// A version of the FzzxBuzz problem. Not limiting numbers from 1 to 100. The gist: for multiples of three print “Fizz” for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

let numberInput = document.getElementById('number'); 
let button = document.getElementById('the-button');
let resultBox = document.getElementById('result');


let fizzBuzz = function(n) {
        if(n % 3 == 0 && n % 5 == 0) {
          console.log(`${n} is FizzBuzz`);
          return 'FizzBuzz';
        } else if (n % 3 == 0) {
             console.log(`${n} is Fizz`);
             return 'Fizz';
        } else if (n % 5 == 0) {
             console.log(`${n} is Buzz`);
              return 'Buzz';
        } else {
          console.log(`${n} is not Fizz or Buzz`);
          return 'not Fizz or Buzz';
};
        } 

let whatIsIt = function(event) {
  // Prevent page refresh on submit
  event.preventDefault();
  // Get input value
  let theNumber = numberInput.value;
  // Clear input box
  numberInput.value = '';
  // Evaluate the number
  let whatItIs = fizzBuzz(theNumber);
  // Update result 
  resultBox.textContent = whatItIs;
}

let clearResult = function() {
  result.innerHTML = '<span id="result">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>';
}

// Do the FizzBuzz thingon submit (buttona and input)
button.addEventListener('click', whatIsIt);
numberInput.addEventListener('submit', whatIsIt);
resultBox.addEventListener('click', clearResult);


// Testing
// -------------
// fizzBuzz(3);
// fizzBuzz(5);
// fizzBuzz(15);


// ====================
// THE COMMON PROBLEM:
// ====================

// Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

var fizzBuzz100 = function(n) {
    n = 100;
    for(let i = 1; i <= n; i++) {
      let fizz = i % 3 == 0;
      let buzz = i % 5 == 0;
      
          if(i % 3 == 0 && i % 5 == 0) {
        console.log('FizzBuzz');
    } else if (i % 3 == 0) {
        console.log('Fizz'); 
    } else if (i % 5 == 0) {
        console.log('Buzz');
    } else {
        console.log(i);
    }
  } 
};

fizzBuzz100();





// Do a thing with this idea later
    // switch(i) {
    //   case fizz:
    //     console.log('Fizz');
    //     break;
    //   case buzz:
    //     console.log('Buzz');
    //     break;
    //   case fizz && buzz:
    //     console.log('FizzBuzz');
    //     break;
    //   default: 
    //     console.log(i);  
    // }
  

              
            
!
999px

Console