Rock, paper scissors


#1

i finished the function and it worked on console, but i tried to run it on browser and it wont run.
If someone got time to check on my code and tell me why it wont run

<!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xthml1/DTD/xhtml-transitional.dtd">

<style type="text/css">

</style>

JavaScript




#2

You need to post all of your code. :slight_smile:


#3

<!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xthml1/DTD/xhtml-transitional.dtd">

<style type="text/css">

</style>

JavaScript




#4

it wont paste it, i will think of some way show it :smiley:


#5

<!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xthml1/DTD/xhtml-transitional.dtd">
 <html>
 <head>
 
  	<style type="text/css">
		
	</style>
	
  <title>JavaScript
 </title>
 
 </head>
  <body>
  
  <script type="text/javascript">
	var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2)
                {
                    if(choice1 === choice2)
                     {
                      return "The result is a tie!"
                     }
                else if (choice1 === "rock")
                    {
                    if(choice2 === "scissors")
                        {
                        return "rock wins"
                        }
                    else(choice2 === "paper")
                        {
                         return "paper wins"    
                         }
                    }
                else if(choice1 === "paper")
                    {
                    if (choice2 === "rock")
                        {
                        return "paper wins"
                        }
                    else (choice2 === "scissors")
                        {
                        return "scissors wins"
                        }
                    }
                else (choice1 === "scissors")
                    {
                    if (choice2 === "rock")    
                        {
                        return "rock wins"
                        }
                    else (choice2 === "paper")
                        {
                        return "scissors wins"
                        }
                    }
                }
    compare(userChoice,computerChoice)
                    
                
	
                    
                
    
  </script>
  
   
  
</body>
</html>

#6

The only reason it's not showing is because you haven't formatted, read about how to do that here,


#7

It's works only in console, but your html doesn't have any tags to display. You need to add div tag for example and using DOM and JS (or jQuery) set the value you need.


#8

<!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xthml1/DTD/xhtml-transitional.dtd">
 <html>
 <head>
 
    <style type="text/css">
    
  </style>
  
  <title>JavaScript
 </title>
 
 </head>
  <body>
  
  <p id="text"></p><!-- i added p element tags -->



  <script type="text/javascript">
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  var text = document.getElementById("text");//<-----I added the script up the p tag above

var computerChoice = Math.random();
if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if(computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2)
                {
                    if(choice1 === choice2)
                     {
                      return "The result is a tie!"
                     }
                else if (choice1 === "rock")
                    {
                    if(choice2 === "scissors")
                        {
                        return "rock wins"
                        }
                    else(choice2 === "paper")
                        {
                         return "paper wins"    
                         }
                    }
                else if(choice1 === "paper")
                    {
                    if (choice2 === "rock")
                        {
                        return "paper wins"
                        }
                    else (choice2 === "scissors")
                        {
                        return "scissors wins"
                        }
                    }
                else (choice1 === "scissors")
                    {
                    if (choice2 === "rock")    
                        {
                        return "rock wins"
                        }
                    else (choice2 === "paper")
                        {
                        return "scissors wins"
                        }
                    }
                }
    

    text.innerHTML = compare(userChoice,computerChoice);// <--- i outputed your value
                    
</script>

#9

Thx a lot, still learning and havent leaned about getElementById :frowning:


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.