Question: 6 . 1 7 LAB: JavaScript number guessing game Download the ZIP file below containing HTML and JavaScript files. Write a function called playGuessingGame (
LAB: JavaScript number guessing game
Download the ZIP file below containing HTML and JavaScript files. Write a function called playGuessingGame in script.js that has two parameters:
numToGuess is the number that the user has to guess.
totalGuesses is the total number of times the user is allowed to guess the number. The default value should be
The playGuessingGame function should return the number of guesses the user took to enter the correct number. Ex: If the user guesses the correct number after tries, the function should return If the user does not guess the correct number before the number of guesses exceeds totalGuesses, the function should return
The playGuessingGame function should call the JavaScript prompt function to read the user's input, as shown below.
Screenshot of example prompt
The prompt text should reflect what the user previously entered:
If the user is making their first guess, the prompt should read: "Enter a number between and
If the user previously guessed a number numToGuess, the prompt should read X is too small. Guess a larger number.", where X is the number previously entered.
If the user previously guessed a number numToGuess, the prompt should read X is too large. Guess a smaller number.", where X is the number previously entered.
If the user enters an empty string or a string that is not a number, the prompt should read "Please enter a number." and give the user another chance to enter another number without losing a turn. Hint: Use isNaN
If the user presses Cancel, playGuessingGame should immediately return without prompting for any more numbers.
Ex: The function call playGuessingGame allows the user guesses the default to guess the number The function call playGuessingGame allows the user guesses to guess the number
To test your code in your web browser, call playGuessingGame from the JavaScript console.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
