Skip to content

Instantly share code, notes, and snippets.

@austntatious
Created October 9, 2015 09:54
Show Gist options
  • Select an option

  • Save austntatious/0d4a7783929e3aa9a0f9 to your computer and use it in GitHub Desktop.

Select an option

Save austntatious/0d4a7783929e3aa9a0f9 to your computer and use it in GitHub Desktop.
Math browser game with prompts and alert to guess user's number
var startGame = function() {
alert("Welcome to the Guess Your Digit Game!");
var startOrNot = confirm("Would you like to start?");
if (startOrNot === true) {
alert("Okay, let's have you start by choosing a 4 digit number. Click OK when you've written it down.");
alert("Now, you're going to add all the digits of your number together and record it.");
alert("Next, subtract your new sum from the original number. Press OK when ready.");
var numberInput = prompt("Finally, remove a digit from this number that's not a zero " +
"- I'm going to try to guess what the digit you removed is. I'll need one hint though, what are" +
"the 3 digits that are remaining in your number?");
}
function findDigitRemoved() {
var sum = 0;
for (var i = 0; i < numberInput.length; i++) {
sum += parseInt(numberInput.charAt(i), 10);
}
var findMultNine = function() {
var multNine = sum;
while (multNine % 9 !== 0 && multNine !== 0) {
multNine++;
}
return multNine;
};
var digitRemoved = findMultNine() - sum;
console.log("The digitRemoved var is equal to" + digitRemoved);
console.log("The sum variable is equal to" + sum);
console.log(findMultNine() + "is the multNine variable");
return digitRemoved;
}
var endGame = confirm("Hmmm. The digit that you removed was " + findDigitRemoved() + ". Was I right?" +
" Press OK to play again.");
if (endGame === true) {
startGame();
} else
alert("Thanks for playing!");
};
startGame();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment