所以我开始学习 HTML/CSS/JavaScript 并在尝试制作一个非常简单的石头剪刀布版本时遇到了这个问题。我相信代码工作正常,但我无法检查,因为它拒绝执行。我在互联网上广泛寻找答案,但似乎找不到任何答案。我在 JavaScript 方面的经验很少,我目前正在从Codecademy学习它,但我认为该资源可能已经过时,因为其他网站似乎有语法冲突。简而言之,我做错了什么,哪个网站做对了?
<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>
**编辑:Codecademy 的词汇表似乎与其他网站一致,他们只是还没有开始编辑他们的课程。*
**编辑:这是我最后的小代码。享受它的简单!*
<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>