JavaScript Project – Guess the Number Game

Free Web development courses with real-time projects Start Now!!

Program 1

<html>
    <head><title>Guess the Number Game </title>
        <script type="text/javascript">
            
            const compchoice=Math.floor(Math.random()*100)+1
            let count=5
            let i=0
            let temp=true
            function guess_number()
            {
                let userchoice=parseInt(document.getElementById("txtuser").value)
                // alert("User Choice: "+ userchoice)   
                // alert("Computer Choice: "+ compchoice)   
                 i++
                count--
                if(userchoice>compchoice)
                  alert("Your Number is Grater...! now "+ count + " chance left")
               else if(userchoice<compchoice)
                  alert("Your Number is Less...! now "+ count + " chance left")
              else if(userchoice==compchoice)
            { 
                  document.writeln("<font color=#cd780e size=5>Congrats You won the game.....!</font>")
                  document.writeln("<br><br><center><font color=green size=5>If You want to play more refresh your page or click Below.....!</center>")
                  document.writeln("<br><center><A href=GuessNumber.html>click here to start</center>")  
                  return
            }    
              if(count==0)
               {
                document.writeln("<br><br><center><font color=green size=5>Oops You lose the game.....!</font></center>")
                document.writeln("<center><font color=#992506  size=5>Actual Number is: </font><font color=red size=5>"+compchoice+"</font></center>")   
                document.writeln("<br><br><center><font color=green size=5>If You want to play more refresh your page or click below.....!</font></center>")
                document.writeln("<br><center><A href=GuessNumber.html>click here to start</center>")  
                return
               }  
            }
         </script>   
     </head>
     <body>
          <center>
            <br><br><br>
            <font color=#a41f05 size="7">Guess the Number Game</font>
            <form>
            <table border="1">
                   <tr>
                    <th><font color=#cd780e size="5">Enter A Number for Guess(1-100):</font></th>
                    <td><input type=text id="txtuser"></td>
                    <td><input type="button" value="Click Here" onclick="return guess_number()"></td>
                   </tr>
            </table>   
            </form>
          </center> 
     </body>
</html>

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *