As I delve into learning HTML/CSS/JavaScript, I encountered a problem while attempting to create a basic rock-paper-scissors game. Despite believing that my code is correct, it simply won't run. Searching the internet for solutions has yielded no results. With limited JavaScript experience, I'm currently honing my skills through Codecademy. However, it seems that different websites offer conflicting syntactical advice. At this point, I'm unsure of what went wrong and which source is accurate.
<html>
<head>
<title>R,P,S!</title>
<script type="text/javascript">
function whole(){
function game(play){
if (play="yes"){
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";
}
function compare(choice1,choice2){
if (choice1==choice2){
compare(userChoice,computerChoice);
}
if (choice1=="rock"){
if (choice2=="scissors"){
document.getElementById("result").innerHTML="Rock wins";
}
else{
document.getElementById("result").innerHTML="Paper wins";
}
}
if (choice1=="paper"){
if (choice2=="rock"){
document.getElementById("result").innerHTML="Paper wins";
}
else{
document.getElementById("result").innerHTML="Scissors win";
}
}
if (choice1=="scissors"){
if (choice2=="paper"){
document.getElementById("result").innerHTML="Scissors win";
}
else{
document.getElementById("result").innerHTML="Rock wins";
}
}
};
compare(userChoice,computerChoice);
}
else{
document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
}
}
var start = prompt ("Do you want to play?","Yes");}
</script>
</head>
<body style="text-align:center">
<h1>JavaScript on a webpage? This is madness!</h1>
<h2>Madness? THIS... IS... HTML!!!!</h2>
<button onclick="whole()">Try it out!</button>
<p id="result">Who won?</p>
</body>
</html>
**Edit: It appears that Codecademy's glossary agrees with other websites, they just haven't gotten around to editing their lessons yet.*
**Edit: Here's my final little code for it. Enjoy its simplicity!*
<html>
<head>
<title>R,P,S!</title>
<script type="text/javascript">
function whole(){
function game(play){
if (play=="Yes"||play=="yes"){
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";
}
function compare(choice1,choice2){
if (choice1==choice2){
alert("It was a tie!");
game("yes");
}
if (choice1=="rock"){
if (choice2=="scissors"){
document.getElementById("messages").innerHTML="";
document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
document.getElementById("loss").innerHTML="";
}
else{
document.getElementById("messages").innerHTML="";
document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
document.getElementById("win").innerHTML="";
}
}
else if (choice1=="paper"){
if (choice2=="rock"){
document.getElementById("messages").innerHTML="";
document.getElementById("win").innerHTML="You win. Paper smothers rock.";
document.getElementById("loss").innerHTML="";
}
else{
document.getElementById("messages").innerHTML="";
document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
document.getElementById("win").innerHTML="";
}
}
else if (choice1=="scissors"){
if (choice2=="paper"){
document.getElementById("messages").innerHTML="";
document.getElementById("win").innerHTML="You win. Scissors cut paper.";
document.getElementById("loss").innerHTML="";
}
else{
document.getElementById("messages").innerHTML="";
document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
document.getElementById("win").innerHTML="";
}
}
else{
alert("Very funny. Now do it right.");
game("yes");
}
};
compare(userChoice,computerChoice);
}
else{
document.getElementById("messages").innerHTML="Well alrighty then.";
document.getElementById("loss").innerHTML="";
document.getElementById("win").innerHTML="";
}
}
var start = prompt ("Do you want to play?","Yes");
game(start);}
</script>
<style>
body{
text-align:center;
}
#messages{
font-size:20px;
color: #00246B;
}
#win{
color: #29A329;
font-size:18px;
}
#loss{
color:#CC0000;
font-size:18px;
}
a{
text-decoration:none;
color:black;
}
a:hover{
font-size:125%;
color:#B20000;
}
button{
font-size:21px;
}
</style>
</head>
<body>
<a href="http://youtu.be/T8r3cWM4JII">
<h1>JavaScript on a webpage? This is madness!</h1>
<h2>Madness? THIS... IS... HTML!!!!</h2>
</a>
<button onclick="whole()">Try it out!</button>
<p id="messages">Who won?</p>
<p class="result"><span id="loss"></span><span id="win"></span></p>
</body>
</html>