Number Guessing Game Javascript | Guess Number Javascript

Number Guessing Game Javascript | Guess Number Javascript

Welcome🎉 to Code With Random blog. This blog teaches us how we create a Number Guessing Game. We use HTML, Css, and javascript for the Number Guessing Game. I hope you enjoy our blog so let’s start with a basic HTML structure for the Number Guessing Game. 


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"> 
    <title>Guess Number</title>
    <link rel="stylesheet" href="style1.css">
        <h1>Guess My Number</h1>
        <p id="header">I'm a number between 1 and 100</p>
        <h4># of times you've guessed:<p id="guesses"></p></h4>
        <form id="thegame" action="" method="get">
            <input type="text" pattern="\d*" id="guess" maxlength="3" />
            <input type="submit" value="Guess" />
        <a href="#">New game</a>
    <script src=""></script>
  <script src="app.js"></script>  

There is all the HTML code for the Number Guessing Game. Now, you can see an output with a Number Guessing Game then we write javascript for the Number Guessing Game.


Guess the Number game javascript

CSS Code

html, body {
  padding: 25px 0;
  margin: 0;
  font: normal 16px "Helvetica Neue", Arial, sans-serif;
  text-align: center;
  color: #16997c;
  background: url("") no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;

main {
  display: block;
  max-width: 400px;
  min-width: 250px;
  padding: 20px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 8px;
  box-sizing: border-box;

@media (max-width: 480px) {
  main {
    width: 90%;

h1 {
  color: #d35400;

fieldset {
  padding: 0 0 25px 0;
  margin: 0;
  border: none;

input[type="text"] {
  width: 40px;
  height: 40px;
  padding: 10px;
  margin: 0 0 20px 0;
  font: normal 20px "Helvetica Neue", Arial, sans-serif;
  text-align: center;
  border: none;
  box-sizing: border-box;

input[type="submit"] {
  display: block;
  width: 50%;
  margin: 0 auto;
  text-align: center;
  padding: 15px 0;
  font: normal 20px Helvetica, Arial, sans-serif;
  color: #ffffff;
  text-shadow: 1px 1px 5px rgba(80, 80, 80, .8);
  border: none;
  background: linear-gradient(to bottom, #f39c12 0%, #d35400 100%);
  -webkit-box-shadow: 0 15px 10px #777;
  -moz-box-shadow: 0 15px 10px #777;
  box-shadow: 0 15px 10px #777;

a { 
  color: #d35400; 
  font-size: 1.4em;
  text-decoration: none;
  font-weight: bold;

#guess {
  width: 90px;

Css Updated output

Guess number game js

Javascript Code

$(document).ready(function() {
  var answer = Math.floor(Math.random() * 100) + 1;

  var guess_count = 0;

  // on submit even for the guess
  $("form").on("submit", function(e) {

    var guess = +$("input#guess").val();

    // comparisons to see if guess is right
    if (guess > answer) {
      var high_message = "My number is less than " + guess;

    } else if (guess < answer) {
      var low_message = "My number is greater than " + guess;
    } else {
      var congrat_message = "Congratulations " + guess + " is the number";

  // on click event to reset the game
  $("a").on("click", function(e) {

    answer = Math.floor(Math.random() * 100) + 1;
    $("p").text("Guess a number from 1 to 100");
    guess_count = 0;


Final output

guessing game javascript code

guess my number javascript

Now that we have completed our javascript section, our updated output with javascript. Hope you like the number guessing game. you can see the output video and project screenshots. See our other blogs and gain knowledge in front-end development. Thank you!
This post teaches us how to create a number guessing game using simple HTML & CSS and javascript. If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning.

Written by – Code With Random/Anki

Code By – Luke Genco

Check out more…..

1. 50 front-end projects html Css

2. add to cart button

3. quiz app javascript

Share on:

Leave a Comment