Last active
August 29, 2015 14:05
-
-
Save adamgiese/55e13fdb441efe0ca4bb to your computer and use it in GitHub Desktop.
JavaScript FizzBuzz Example
This file contains hidden or 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> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> | |
<title>FizzBuzz</title> | |
</head> | |
<body> | |
<div id="Fizzy-Buzz"> | |
</div> | |
</body> | |
<script> | |
var fizzList = document.getElementById("Fizzy-Buzz"); | |
function printFizzBuzzValue(entry) { | |
var message = document.createElement("p"); | |
var text = document.createTextNode(entry); | |
message.appendChild(text); | |
fizzList.insertBefore(message, fizzList.firstChild); | |
}; | |
for (i = 1; i <= 100; i++) { | |
var printedValue = ''; | |
if (i % 3 == 0) { | |
printedValue = 'Fizz'; | |
} | |
if (i % 5 == 0) { | |
printedValue += 'Buzz'; | |
} | |
printFizzBuzzValue(printedValue || i); | |
} | |
</script> |
Also, I would say "printFizz" is a bad function name. Good function and variable names are super important for code readability. Can you think of a better name?
the edits look great Adam! that is how I would have done it. I like your use of "or"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This all looks great adam! Can you think of a way to combine the three if statements into two?