I got a simple program that get some input from the user and the computer in a simple Rock scissor paper game. I am supposed to compare the input from the user and then compare with the output of the
Everything is fine, the variable computer shows the correct output but when I assign to variable cs which is supposed to take the first character of computer, it always shows the same output as the player ps, therefore the result is always a tie, which is wrong.
Can any of you guys tell me why and how should I resolve this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame
Remove this if you use the .htaccess -->
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Complete Java Script Course</title>
<meta name="description" content="">
<meta name="author" content="Yuan">
<meta name="viewport" content="width=device-width; initial-scale=1.0">
<!-- Replace favicon.ico & apple-touch-icon.png in the root of your domain and delete these references -->
<link rel="shortcut icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
</head>
<body>
<div id="output">Complete Java Script Course</div>
<script>
var output = document.getElementById("output");
var player = prompt("Do you Choose Rock, Paper or Scissors?")
var ps = player.charAt(0).toUpperCase();
var win = false;
var computer = Math.random();
if(computer < 0.34){computer ="Rock";}
else if (computer < 0.67){computer ="Paper";}
else{computer= "Scissors";}
var cs = computer.charAt(0).toUpperCase();
console.log(computer);
var html="";
if (cs=ps){
html+="Its a tie!";}
else{
if(ps=="R"){
if(cs=="S"){win=true;}
}
else if(ps=="P"){
if(cs=="R"){win=true;}
}
else{
if(cs=="P"){win=true;}
}
if(win){
html += "Player Wins!";
} else{
html+= "Computer Wins!";
}
}
output.innerHTML = "Player Vs Computer<br>"+ player+" "+ "vs" +" "+computer+"<br>Result:"+" "+html;
</script>
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire