Question

In: Computer Science

(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses...

(HTML) Write a script that plays a “guess the number” game as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The script displays the prompt Guess a number between 1 and 1000 next to a text field. The player types a first guess into the text field and clicks a button to submit the guess to the script. If the player's guess is incorrect, your program should display either Too high, try again or Too low, try again to help the player “zero in” on the correct answer and should clear the text field so the user can enter the next guess. When the user enters the correct answer, display Congratulations! You guessed the number! and clear the text field so the user can play again.

Solutions

Expert Solution

HTML & JS CODE:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Number Guessing Game</title> 
  
    <style> 
        html { 
    font-family: sans-serif; 
    } 
      
        body { 
    width: 50%; 
    max-width: 800px; 
    min-width: 480px; 
    margin: 0 auto; 
    } 
    </style> 
</head> 
  
<body> 
<h1>Guess The Number</h1> 
  
<p>We have selected a random number between 1 - 1000.  
See if you can guess it.</p> 
  
<div class="form"> 
    <label for="guessField">Enter a guess: </label> 
    <input type = "number" id = "guessField" class = "guessField" min="1" max="1000"> 
    <input type = "submit" value = "Submit guess" 
           class = "guessSubmit" id = "submitguess"> 
           
</div> 
  
<script type = "text/javascript"> 
  
    // random value generated 
    var y = Math.floor(Math.random() * 10 + 1); 

    // counting the number of guesses 
    // made for correct Guess 
    var guess = 1; 

      
    document.getElementById("submitguess").onclick = function(){ 
      
   // number guessed by user      
   var x = document.getElementById("guessField").value; 
  
   if(x == y) 
   {     
       alert("Congratulations! You guessed the number! "
               + guess + " GUESS "); 
   } 
   else if(x > y) /* if guessed number is greater 
                   than actual number*/ 
   {     
       guess++; 
       alert("Too high, try again"); 
   } 
   else
   { 
       guess++; 
       alert("Too low, try again");
   }

   document.getElementById("guessField").value='';
} 
</script> 
</body> 
</html>

Related Solutions

Write a program with at least 2 functions that play the game of “guess the number”...
Write a program with at least 2 functions that play the game of “guess the number” as follows: Your program chooses the number to be guessed by selecting an integer at random in the range 1 to 1000. The program then displays the following: I have a number between 1 and 1000. Can you guess my number? Please type in your first guess. The player then types the first guess. The program then responds with one of the following: 1.      ...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Develop a Java application that plays a "guess the number" game as described below. a) The...
Develop a Java application that plays a "guess the number" game as described below. a) The user interface is displayed and the user clicks the “Start Game” button to begin the game. b) Your application then gets a random number in the range 1-1000 inclusive (you might want to use Math.random or the Random class). c) The application then displays the following prompt (probably via a JLabel): I have a number between 1 and 1000 can you guess my number?...
Create a Guess the Number game. Python randomly chooses a secret four-digit number (with no repeating...
Create a Guess the Number game. Python randomly chooses a secret four-digit number (with no repeating digits) and asks the user to guess it. The user has up to ten attempts to guess the number. After each guess, Python gives the user two clues (until the user either guesses the number correctly or runs out of attempts): The number of digit(s) in the user’s guess that is (are) both correct and in the right position. The number of digit(s) in...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
using matlab Write a script that simulates a card game that works as follows: A dealer...
using matlab Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card. The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess. In terms of coding, your script should...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random...
Program Created Last Week: #Program plays a guessing the number game with the user. #Importing random number with min number being 1 and max being 10 import random min_number = 1 max_number = 10 rand = random.randint(min_number, max_number) #Prints the header, welcoming the user print("Welcome to the Guess My Number Program!") while (True): #While loop, comparing the users guessed number with the random number. #If it matches, it will say you guessed it. guess = eval(input("Please try to guess my...
Write a game where a user has to guess a number from 1 – 6, inclusive....
Write a game where a user has to guess a number from 1 – 6, inclusive. Your program will generate a random number once (pick a number), and will then prompts the user to guess the number for up to 3 times. If the user enters 3 wrong guesses, the program should be terminated along with the losing message. Once the user has successfully guessed the number, tell the user they win, and tell them how many guesses it took...
1. Write a program that plays a simple dice game between the computer and the user....
1. Write a program that plays a simple dice game between the computer and the user. When the program runs, it asks the user to input an even number in the interval [2..12], the number of plays. Display a prompt and read the input into a variable called ‘plays’ using input validation. Your code must ensure that the input always ends up being valid, i.e. even and in [2..12]. 2. A loop then repeats for ‘plays’ iterations. Do the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT