ExtremeGamer1480

JavaScript Guessing Game

Jun 6th, 2021
667
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function guessinggame() {
  2.     if (!confirm('Do you want to play a number guessing game?'))
  3.         return;
  4.     var minnum,
  5.         maxnum,
  6.         totalguesses = 0,
  7.         guessnumber,
  8.         guess = 0;
  9.     definevalues();
  10.  
  11.     function definevalues() {
  12.         minnum = prompt('Please input the smallest possible number.');
  13.         if (minnum == null)
  14.             return;
  15.         if (Number.isInteger(Number(minnum))) {
  16.             maxnum = prompt('Please input the largest possible number.');
  17.             if (maxnum == null)
  18.                 return;
  19.             if (Number.isInteger(Number(maxnum))) {
  20.                 if (Number.isSafeInteger(Number(minnum)) && Number.isSafeInteger(Number(maxnum))) {
  21.                     guessnumber = Math.round(Math.random() * (maxnum - minnum) + minnum);
  22.                     totalguesses = 0,
  23.                         guess = '';
  24.                     startgame();
  25.                 } else {
  26.                     alert(`Integer Overflow!\n\nPlease keep your 'Smallest possible number' over ${Number.MIN_SAFE_INTEGER} and your 'Largest possible number' under ${Number.MAX_SAFE_INTEGER}`);
  27.                 }
  28.             } else {
  29.                 alert('Invalid Input!\nPlease type a whole number.')
  30.             }
  31.  
  32.         } else {
  33.             alert('Invalid Input!\nPlease type a whole number.');
  34.         }
  35.     }
  36.  
  37.  
  38.     function startgame() {
  39.         totalguesses += 1
  40.         guess = prompt(`The number is between ${minnum} and ${maxnum}\n\nTry to guess the number!`, guess);
  41.         if (guess == '\x64\x65\x76') {
  42.             totalguesses -= 1;
  43.             guess = guessnumber;
  44.             startgame();
  45.             return;
  46.         }
  47.         if (guess == guessnumber) {
  48.             if (guess == null)
  49.                 return;
  50.             if (totalguesses == 1) {
  51.                 var playagainornah = confirm(`Congrats! You have guessed the number in 1 guess!\nThat's some real luck!\n\nThe number was ${guessnumber}.\nDo you want to play again?`);
  52.                if (!playagainornah)
  53.                    return;
  54.                totalguesses = 0
  55.                definevalues();
  56.            } else {
  57.                var playagainornah = confirm(`Congrats! You have guess the number in ${totalguesses} guesses!\n\nThe number was ${guessnumber}.\nDo you want to play again?`);
  58.                if (!playagainornah)
  59.                    return;
  60.                totalguesses = 0
  61.                definevalues();
  62.            }
  63.  
  64.        } else if (guess > guessnumber) {
  65.            alert('Smaller!');
  66.            startgame();
  67.            return;
  68.        } else if (guess < guessnumber) {
  69.            alert('Bigger!');
  70.            startgame();
  71.            return;
  72.        } else {
  73.            alert('Invalid Input!\nIf you are trying to make a guess with numbers in a word form, please type the numbers in number form.');
  74.            totalguesses -= 1
  75.            startgame();
  76.            return;
  77.        }
  78.    }
  79.  
  80. }
  81. guessinggame();
RAW Paste Data