Skip to content

Instantly share code, notes, and snippets.

@yi-mei-wang
Last active October 1, 2019 12:59
Show Gist options
  • Save yi-mei-wang/b858d49af0e925d87a9b68cc8adf1ecc to your computer and use it in GitHub Desktop.
Save yi-mei-wang/b858d49af0e925d87a9b68cc8adf1ecc to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>FIZZ BUZZ WOOOOOF</title>
</head>
<body>
<button onclick="playGame()">PLAY GAME</button>
<script src="fizzbuzz.js"></script>
</body>
</html>
let output = "";
const fizzBuzzWoof = {
"3": "Fizz ",
"5": "Buzz ",
"7": "Woof "
};
function convertNum(userInput) {
// Loop through the fizzBuzzWoof object
// num is 3, 5, or 7
for (num in fizzBuzzWoof) {
// Check if userInput is divisible by 3, 5, or 7
if (userInput % num === 0) {
output += fizzBuzzWoof[num];
}
// Check if userInput contains 3, 5, or 7
if (userInput.includes(num)) {
output += fizzBuzzWoof[num];
// Check if userInput contains 33, 55 or 77
if (userInput.includes(num.repeat(2))) {
output += fizzBuzzWoof[num];
}
}
}
return output;
}
function checkIfValid(userInput) {
// A function that checks if user's input is valid
// If it is not, they will be alerted and false will be returned
if (isNaN(userInput)) {
alert("I WANT A NUMBER NOT GIBBERISH");
return false;
}
// Alert user if their input is too large
else if (userInput > 100) {
alert("GIMME A LOWER NUMBER OIE");
return false;
}
return true;
}
function playGame() {
let userInput;
// Continuously prompt user if their input is not valid
// QUESTION: what does a do-while loop do?
// I want you to research what it does and why and when do we use a do-while loop
do {
// Get a number from the user and store inside a variable
userInput = prompt("GIMME A NUMBER BABYYYY");
} while (!checkIfValid(userInput));
alert(convertNum(userInput));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment