Tuesday, April 8, 2025

Rock Paper Scissor Game Using Javascript

<script>

    function random(){

      var item = ["Rock","Paper","Sessior"];

     var rand =  item[Math.floor(Math.random()*item.length)];

     return rand;

 

    }

 

    function r(){

        var rock = document.getElementById("r").value;

        var computerinput = random();

       

        if(rock=="Rock" && computerinput=="Rock")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>Match Tie</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+rock+"</b>";

           

        }

        else if(rock=="Rock" && computerinput=="Paper")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>Computer Win!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+rock+"</b>";

        }

        else if(rock=="Rock" && computerinput=="Sessior")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>User Win!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+rock+"</b>";

        }

 

    }



 

    function p(){

       

        var paper = document.getElementById("p").value;

        var computerinput = random();

       

        if(paper=="Paper" && computerinput=="Rock")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>Computer Win!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+paper+"</b>";

           

        }

        else if(paper=="Paper" && computerinput=="Paper")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>Match Tie!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+paper+"</b>";

        }

        else if(paper=="Paper" && computerinput=="Sessior")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>Computer Win!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+paper+"</b>";

        }

 

    }


 

    function s(){

       

        var sessior = document.getElementById("s").value;

        var computerinput = random();

       

        if(sessior=="Sessior" && computerinput=="Rock")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>COmputer Win!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+sessior+"</b>";

           

        }

        else if(sessior=="Sessior" && computerinput=="Paper")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>User Win!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+sessior+"</b>";

        }

        else if(sessior=="Sessior" && computerinput=="Sessior")

        {

            document.getElementById("result").innerHTML="<b style='color:steelblue'>Match Tie!</b>"+"Computer Input is <b style='color:red;'>"+computerinput+"</b> User Input is <b style='color:green;'>"+sessior+"</b>";

        }

 

    }

</script>


 

<center>

<input type="button" id="r" onclick="r()" value="Rock">

<input type="button" id="p" onclick="p()" value="Paper">

<input type="button" id="s" onclick="s()" value="Scissor">

 

<br>

<br>

<div id="result"></div>

</center>


Video Tutorial

 

No comments:

Post a Comment

Variable , Assign multiple variable in python with short program

  See video -  Go to video Code : # variable x = 7 y = 5 z = 9 print ( x + y + z ) #assign multiple variable x , y , z = "hello...