Skip to content

Instantly share code, notes, and snippets.

@elliotkim916
Created November 18, 2017 22:35
Show Gist options
  • Save elliotkim916/3823108d0c14f46a0bc844ae624b2239 to your computer and use it in GitHub Desktop.
Save elliotkim916/3823108d0c14f46a0bc844ae624b2239 to your computer and use it in GitHub Desktop.
Event Listener Drills
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>fizzbuzz drill</title>
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<main>
<h1>Fizz Buzz</h1>
<section>
<p>In this game, you enter a positive number, and I count from 0 to that number, subbing in "fizz" if it's divisible by 3, "buzz" if it's divisible by 5, and "fizzbuzz" if it's divisible by both</p>
<form id="number-chooser" action="www.somewhere.com">
<label for="number-choice">Choose a positive number</label>
<input id="number-choice" name="number-choice" type="number" required />
<input type="submit" />
</form>
</section>
<section>
<h2>Results</h2>
<div class="js-results"></div>
</section>
</main>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="index.js"></script>
</body>
</html>
function fizzbuzzAgain() {
$('#number-chooser').submit(function(event) {
event.preventDefault();
$('.js-results').empty();
let userNumber = $(event.currentTarget).find('#number-choice').val();
let results = [];
for (let i=1; i<=userNumber; i++) {
if (i % 15 === 0) {
results.push($('<div class="fizz-buzz-item fizzbuzz"><span>fizzbuzz</span></div>'));
} else if (i % 5 === 0) {
results.push($('<div class="fizz-buzz-item buzz"><span>buzz</span></div>'));
} else if (i % 3 === 0) {
results.push($('<div class="fizz-buzz-item fizz"><span>fizz</span></div>'));
} else {
results.push($(`<div class="fizz-buzz-item"><span>${i}</span></div>`));
}
}
$('.js-results').append(results);
});
}
$(fizzbuzzAgain());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment