Skip to content

Instantly share code, notes, and snippets.

@adamgiese
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamgiese/55e13fdb441efe0ca4bb to your computer and use it in GitHub Desktop.
Save adamgiese/55e13fdb441efe0ca4bb to your computer and use it in GitHub Desktop.
JavaScript FizzBuzz Example
<!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>
@johndgiese
Copy link

This all looks great adam! Can you think of a way to combine the three if statements into two?

@johndgiese
Copy link

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?

@johndgiese
Copy link

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