<div id="container">
<form id="fizzbuzz">
<h2>Fizzbuzz</h2>
<p>
Write a program that has a starting and finishing number.</p>
<ul>
<li>Any multiples of three (3) print “Fizz” instead of the number.</li>
<li>Any multiples of five (5) print “Buzz”.</li>
<li>For numbers which are multiples of both three (3) and five (5) print "FizzBuzz”.</li>
</ul>
<input type="text" name="start" id="start" placeholder="Starting Number">
<input type="text" name="finish" id="finish" placeholder="Finishing Number">
<br>
<input type="submit" value="GO!" id="submit">
</form>
<div id="output"></div>
<div id="error"></div>
</div>
$(function() {
$("#fizzbuzz").submit(function(event) {
// Create a start variable and parse the numbers value
// Create a finish variable and parse the numbers value
var start = parseInt($("#start").val());
var finish = parseInt($("#finish").val());
// check that the input values are numbers
if (!isNaN(start) && !isNaN(finish)) {
// loop through numbers
var i;
for (i = start; i <= finish; i++) {
if (i % 3 === 0 && i % 5 === 0) {
$("#output").append("<span class='fizzbuzz'>FizzBuzz</span>");
} else if (i % 3 === 0) {
$("#output").append("<span class='fizz'>Fizz</span>");
} else if (i % 5 === 0) {
$("#output").append("<span class='buzz'>Buzz</span>");
} else {
$("#output").append("<span>" + i + "</span>");
}
}
$("#output span").fadeIn(400);
} else {
// if the inputs are not valid numbers
$("#error").append("<p class='error'>Please enter a valid number in both boxes</p>");
}
// prevent form submission
return false;
});
// empty the output & error divs
$('input').on('focus', function() {
$("#output span").fadeOut(400, function() {
$("#output").empty();
});
$("#error").empty();
});
}); // end ready
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.