JavaScript Guessing Game
This is a rudimentary implementation of some JS code. The objective of the game is to enter a (valid) maximum number, and then try to correctly guess the integer value generated between 1 to that maximum. We also record a number of attempts needed to succeed, as well as a means of exiting the game if things get frustrating!
Entering the maximum value
This page opens with a prompt to the user, asking for a value between 1-100. As long as the user doesn't enter a value within those parameters (i.e. until the user enters a VALID number), the prompt will continue incessantly. It is possible to change this, but I won't - for the purposes of education. The code used is as follows:
let maximum = parseInt(prompt('Please enter a value between 1 to 100'));
while(!maximum || maximum < 1 || maximum > 100){
maximum = parseInt(prompt('Try again! Please enter a valid number [1-100]:'));
}
- Points about the code above:
- Note how parseInt is being used. This is because JavaScript will automatically convert numerical input into a string. Using parseInt(...) should generate a numerical value, if the user is entering any.
- Note the use of let maximum = parseInt(prompt('...'); This works very much like int maximum = 0; cin >> maximum; in C++.
- The while loop will remain 'unbroken' or 'true' for as the maximum is either NaN (not a number...i.e. no integer was parsed from the input), or the input was below 1 or above 100
- If no valid input has been entered, then the user receives non-stop requests to 'Try again!'
Generating a random number
We take the user input, using it to generate a random number between 1-N. The way to do this is as follows:
const targetNum = Math.floor(Math.random() * maximum)+1;
- Additional points:
- The Math.random() method generates a floating-point, pseudo-random number that is GREATER THAN 0 and LESS THAN 1.
- Multiplying Math.random() by our variable maximum will give us some value between (say) 0.000001 and 0.000001 below the maximum number.
- Applying Math.floor(...) to this number will basically LOP OFF the values after the decimal point, rounding DOWN - no matter how high the decimal may be. In theory, applying Math.floor to 1.999999999 will give us 1.
- Finally, we add 1 to the result generated. This guarantees a value between 1-N is forthcoming!
Utility
Of less importance in this exercise are the user prompt for a first guess, as well as the (behind the scenes) declaration of an 'attempts/number of guesses' variable:
let guess = parseInt(prompt('Enter your first guess!'));
let attempts = 1;
You already know what The while loop and logic
while (parseInt(guess) !== targetNum) {
if (guess === 'q') break;
attempts++;
if (guess > targetNum)
guess = prompt('Too high! Enter a new guess!');
else
guess = prompt('Too low. Enter a new guess:');
}
- A few quick points:
- Firstly, note how the break statement is on the same line as our conditional, (guess==='q). This will exit the loop when 'q' is entered.
- The attempts variable automatically increments while the value entered does NOT equal the randomly generated 1-N number :)
- If the guess is larger than the randomly generated target, we guess again, with a prompt to the user
- The same applies if the guess is lower than the target!
Explaining the outcome
This is fairly easy! It explains whether the user quit, or correctly guessed the number.
if (guess === 'q') {
console.log('Okay, quitter!');
} else {
console.log('A winner is you!');
console.log(`You took ${attempts} guesses to get it!`);
}
The only real note here: the use of template literals, i.e. console.log(`You took
${attempts} to get it!`);