number guessing game javascript | number guess game javascript game

 number guessing game javascript | number guess game javascript game





number guessing game javascript | number guess game javascript game



Welcome🎉 to Code With Random blog. In this blog, we learn that how we how to create number guessing game. We use HTML & CSS and javascript for this number guessing game. Hope you enjoy our blog so let's start with a basic HTML structure for number guessing game.

HTML code for number guessing game

 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
   <meta charset="UTF-8">  
   <meta http-equiv="X-UA-Compatible" content="IE=edge">  
   <meta name="viewport" content="width=device-width, initial-scale=1.0">  
   <title>Document</title>  
   <link rel="stylesheet" href="style.css">  
   <link rel="preconnect" href="https://fonts.gstatic.com">  
 <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">  
 </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 Guesss : 0</p>  
       <p id="message2">Guessed number are : none</p>  
       <p id="message3"></p>  
   </div>  
   <script src="app.js"></script>  
 </body>  
 </html>  
There is all HTML code for the number guessing game. Now, you can see output without CSS, then we write CSS for our number guessing game.

number guessing game javascript | number guess game javascript game

CSS for number guessing game

 *{  
   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: #fff;  
 padding: 50px 10px;  
 border-radius: 5px;  
 display: grid;  
 justify-items: center;  
 font-family: 'poppins', sans-serif;  
 }  
 h3{  
   font-size: 30px;  
   font-weight: 600;  
 }  
 input[type="text"]{  
   width: 90px;  
   font-weight: 600;  
   padding: 20px 0;  
   font-size: 28px;  
   text-align: center;  
   margin-top: 30px;  
   border-radius: 5px;  
   border: 2px solid #202020;  
   color: #663399;  
 }  
 button{  
   width: 160px;  
   padding: 15px 0;  
   border-radius: 5px;  
   background-color: #663399;  
   color: #fff;  
   border: none;  
   font-size: 18px;  
   font-weight: 600;  
   margin-bottom: 30px;  
 }  
 p{  
   font-weight: 400;  
 }  

Now we have completed our CSS section,  Here is our updated output CSS.

number guessing game javascript | number guess game javascript game

Now add javascript for the number guessing game!

Javascript 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 guesses_num = [];  
 function play(){  
   var user_guess = document.getElementById("guess").value;  
   if(user_guess < 1 || user_guess > 100 ){  
     alert("Please Enter a number Between 1 to 100");  
   }  
   else{  
     guesses_num.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 Number Are: " +  
  guesses_num;  
     }  
     else if(user_guess > answer){  
       msg1.textContent = "Your Guess is Too High"  
       msg2.textContent = "No. Of Guesses : " +  
       no_of_guesses;  
       msg3.textContent = "Guessed Number Are: " +  
       guesses_num;  
     }  
     else if(user_guess == answer){  
       msg1.textContent = "Yahhhh You won It!!"  
       msg2.textContent = "the Number was " + answer  
       msg3.textContent = " You guessd it in " +  no_of_guesses  +"Guesses";   
     }  
   }  
 }  
Now we have completed our javascript section,  Here is our updated output with javascriptHope you like number guessing game; you can see output video and project screenshots. See our other blogs and gain knowledge in front-end development. Thank you 🙏💕


Post a Comment

Previous Post Next Post