Created
March 1, 2012 14:52
-
-
Save Gab-km/1950264 to your computer and use it in GitHub Desktop.
FizzBuzz with TDD in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function write(message) { | |
document.getElementById("output").innerHTML += message + "<br />"; | |
} | |
function assertEquals(expected, actual) { | |
if (expected == actual) { | |
write("OK"); | |
} else { | |
var text = "expected = [" + expected.toString() | |
+ "], but actual = [" + actual.toString() + "]." | |
write("Failure: " + text); | |
} | |
} | |
function fizzbuzz(number) { | |
if (typeof(number) != "number" || | |
number <= 0) { | |
return "Expected positive numbers."; | |
} | |
if (number % 15 == 0) { | |
return "FizzBuzz"; | |
} else if (number % 3 == 0) { | |
return "Fizz"; | |
} else if (number % 5 == 0) { | |
return "Buzz"; | |
} else { | |
return number.toString(); | |
} | |
} | |
function test_fizzbuzz() { | |
assertEquals("1", fizzbuzz(1)); | |
assertEquals("2", fizzbuzz(2)); | |
assertEquals("Fizz", fizzbuzz(3)); | |
assertEquals("Buzz", fizzbuzz(5)); | |
assertEquals("Fizz", fizzbuzz(6)); | |
assertEquals("Buzz", fizzbuzz(10)); | |
assertEquals("FizzBuzz", fizzbuzz(15)); | |
assertEquals("Expected positive numbers.", fizzbuzz(0)); | |
assertEquals("Expected positive numbers.", fizzbuzz("1")); | |
} | |
function testSuite() { | |
test_fizzbuzz(); | |
} | |
window.onload = testSuite; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>FizzBuzz with TDD in Javascript</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<script type="text/javascript" src="myJsUnit.js"></script> | |
</head> | |
<body> | |
<div id="output" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment