Codecademy javascript 第 7/9 课错误
Codecademy javascript lesson 7/9 error
我已经在 7/9 呆了 2 晚了。这是一款石头剪刀布游戏。我不知道出了什么问题。我尝试了在线 lint,它还说我的第 22 行是一个错误(预期是一个标识符,但却看到了 'else')。按照说明,我在比较函数内的现有代码下写了另一个 else。
我的代码:
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{
return"paper wins";
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
你在 ;
条件之后终止你 else if
应该是:
else if(choice1 ==="paper"){
if(choice2 ==="rock")
return("paper wins");
}
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{
return"paper wins";
}
else if(choice1 ==="paper");{ -- on this there is semicolon after elseif block.. and how come else if is there after else block..
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
我假设你是初学者。编写非常干净的代码,注意空格和制表符,这是解决调试问题的最佳方法。第22行确实有问题,你在条件语句后面加了一个分号。
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 {
return "paper wins";
} else if(choice1 === "paper"){//here the error was.
if(choice2 === "rock")
return("paper wins");
} else {
return "scissors wins";
}
}
我发现您的代码中存在多个语法错误。它应该如下所示:
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 {
return "paper wins";
}
} else if(choice1 ==="paper") {
if(choice2 ==="rock") {
return("paper wins");
} else {
return"scissors wins";
}
}
}
您不能在 else
语句后添加 else if
,这就是导致您显示错误消息的原因 error(Expected an identifier and instead saw 'else')
当然,在行真正结束之前没有分号
https://jsfiddle.net/8hcpfnhw/
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 if(choice1 === "paper"){ // I changed this to else if instead of else
return "paper wins";
}
else if(choice1 ==="paper") {
if(choice2 ==="rock")
return("paper wins");
}
else{ // only else as last check
return"scissors wins";
}
}
我已经在 7/9 呆了 2 晚了。这是一款石头剪刀布游戏。我不知道出了什么问题。我尝试了在线 lint,它还说我的第 22 行是一个错误(预期是一个标识符,但却看到了 'else')。按照说明,我在比较函数内的现有代码下写了另一个 else。
我的代码:
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{
return"paper wins";
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
else if(choice1 ==="paper");{
if(choice2 ==="rock")
return("paper wins");
}
你在 ;
else if
应该是:
else if(choice1 ==="paper"){
if(choice2 ==="rock")
return("paper wins");
}
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{
return"paper wins";
}
else if(choice1 ==="paper");{ -- on this there is semicolon after elseif block.. and how come else if is there after else block..
if(choice2 ==="rock")
return("paper wins");
}
else{
return"scissors wins";
}
}
我假设你是初学者。编写非常干净的代码,注意空格和制表符,这是解决调试问题的最佳方法。第22行确实有问题,你在条件语句后面加了一个分号。
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 {
return "paper wins";
} else if(choice1 === "paper"){//here the error was.
if(choice2 === "rock")
return("paper wins");
} else {
return "scissors wins";
}
}
我发现您的代码中存在多个语法错误。它应该如下所示:
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 {
return "paper wins";
}
} else if(choice1 ==="paper") {
if(choice2 ==="rock") {
return("paper wins");
} else {
return"scissors wins";
}
}
}
您不能在 else
语句后添加 else if
,这就是导致您显示错误消息的原因 error(Expected an identifier and instead saw 'else')
当然,在行真正结束之前没有分号
https://jsfiddle.net/8hcpfnhw/
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 if(choice1 === "paper"){ // I changed this to else if instead of else
return "paper wins";
}
else if(choice1 ==="paper") {
if(choice2 ==="rock")
return("paper wins");
}
else{ // only else as last check
return"scissors wins";
}
}