Skip to content

Instantly share code, notes, and snippets.

@maxxcrawford
Created May 27, 2015 18:53
Show Gist options
  • Save maxxcrawford/18631f5cad1ffd50259e to your computer and use it in GitHub Desktop.
Save maxxcrawford/18631f5cad1ffd50259e to your computer and use it in GitHub Desktop.
FizzBuzz Test Comparison
<!-- 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”." -->
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 80px;
height: 20px;
line-height: 20px;
text-align: center;
outline: 1px solid black;
display: inline-block;
margin: 10px 10px 0 0;
}
.fizz {
background: red;
color: white;
outline: none;
}
.buzz {
background: blue;
color: white;
outline: none;
}
.fizzbuzz {
background: purple;
color: white;
outline: none;
}
</style>
</head>
<body>
<script>
var start = new Date().getMilliseconds();
// For Loop Counting from 1 - 100
for (i = 1; i <= 10000; i++) {
// Test for Multiple of 3
if (i % 3 === 0) {
// Test for Multiple of 3 AND 5
if (i % 5 === 0) {
document.write(" <div class='fizzbuzz'>fizzbuzz</div> ");
} else {
document.write(" <div class='fizz'>fizz</div> ");
}
// Test for Multiple of 5
} else if (i % 5 === 0) {
document.write(" <div class='buzz'>buzz</div> ");
} else {
// If Neither 3 or 5 Multiple, Print Number
document.write("<div>" + i + "</div>");
}
}
var end = new Date().getMilliseconds();
var time = end - start;
console.log('Execution A time: ' + time);
var start2 = new Date().getMilliseconds();
// For Loop Counting from 1 - 100
for (i = 1; i <= 10000; i++) {
three = i%3 === 0;
five = i%5 === 0;
if (three && five) {
// Test for Multiple of 3 AND 5
document.write(" <div class='fizzbuzz'>fizzbuzz</div> ");
}
else if (five) {
// Test for Multiple of 5
document.write(" <div class='fizz'>fizz</div> ");
}
else if (three) {
// Test for Multiple of 3
document.write(" <div class='buzz'>buzz</div> ");
}
else {
// If Neither 3 or 5 Multiple, Print Number
document.write("<div>" + i + "</div>");
}
}
var end2 = new Date().getMilliseconds();
var time2 = end2 - start2;
console.log('Execution B time: ' + time2);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment