Number Guessing Game using JavaScript

Number Guessing Game using JavaScript

Number Guessing Game using JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Number Guessing Game in the user has to guess the number which is been guessed whether it is correct or not. In Today’s Blog, We will use HTML, CSS, and JavaScript to complete Number Guessing Game.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment.

At last, we will use JS (JavaScript) which will add logic to make the project responsive from the user end.

HTML Code for Number Number Guessing Game

<html lang="en">
<head>
    <title>Document</title>
    <!--Google Font-->
    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
   <div class="container">
       <h3>I am thinking of a number between 1-100.</h3>
       <h3>Can you guess it?</h3>

       <input type="text" placeholder="Num" id="guess"><br>
       <button onclick="play()" id="my_btn">GUESS</button>

       <p id="message1">No. Of Guesses: 0</p>
       <p id="message2">Guessed Numbers are: None</p>
       <p id="message3"></p>
   </div>
   <!--Script-->
   <script src="script.js"></script>
</body>
</html>

First we’ll start with creating the structure of the Guessing Number Game project for that as you can see the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

Ecommerce Website Using Html Css And Javascript Source Code

CSS Code for Number Guessing Game

*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    height: 100vh;
    background: linear-gradient(
        to right,
        #7f53ac,
        #657ced
    );
}
.container{
    position: absolute;
    width: 50%;
    min-width: 580px;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    background-color: #ffffff;
    padding: 50px 10px;
    border-radius: 5px;
    display: grid;
    justify-items: center;
    font-family: 'Poppins',sans-serif;
}
h3{
    font-size: 16px;
    font-weight: 600;
}
input[type="text"]{
    width: 90px;
    font-weight: 600;
    color: #663399;
    padding: 20px 0;
    text-align: center;
    margin-top: 30px;
    border-radius: 5px;
    border: 2px solid #202020;
    font-size: 28px;
}
button{
    width: 160px;
    padding: 15px 0;
    border-radius: 5px;
    background-color: #663399;
    color: #ffffff;
    border: none;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 30px;
    outline: none;
}
p{
    font-weight: 400;
}

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Guessing Number Game project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

50+ Html ,Css & Javascript Projects With Source Code

JavaScript Code for Number Guessing Game

var msg1 = document.getElementById("message1");
var msg2 = document.getElementById("message2");
var msg3 = document.getElementById("message3");

var answer = Math.floor(Math.random()*100) + 1;
var no_of_guesses = 0;
var guessed_nums = [];

function play(){
    var user_guess = document.getElementById("guess").value;
    if(user_guess < 1 || user_guess > 100){
        alert("Please enter a number between 1 and 100.");
    }
    else{
        guessed_nums.push(user_guess);
        no_of_guesses+= 1;

        if(user_guess < answer){
            msg1.textContent = "Your guess is too low.";
            msg2.textContent = "No. of guesses: " + no_of_guesses;
            msg3.textContent = "Guessed numbers are: " +
            guessed_nums;
        }
        else if(user_guess > answer){
            msg1.textContent = "Your guess is too high.";
            msg2.textContent = "No. of guesses: " + no_of_guesses;
            msg3.textContent = "Guessed numbers are: " +
            guessed_nums;
        }
        else if(user_guess == answer){
            msg1.textContent = "Yippie You Win!!";
            msg2.textContent = "The number was: " + answer;
            msg3.textContent = "You guessed it in "+ no_of_guesses + " guesses";
            document.getElementById("my_btn").disabled = true;
        }
    }
}

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement. We have also defined the function to let the system analysis the number which is correct or not. Let us see the Final Output of the project Number Guessing Game using HTML, CSS & JavaScript (Source Code).

Final Output Of Number Guessing Game


We have successfully created our Number Guessing Game using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

Restaurant Website Using HTML And CSS With Source Code

If you find out this Blog helpful, then make sure to search code random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Coded By – @harshh9



Leave a Reply