建造"摇滚,纸张,剪刀"在代码codecademy中

时间:2015-07-16 14:17:26

标签: javascript

我使用codeacademy学习基本的JS。

我在" Rock,Paper,Scrissors"

的第6步和第7步
https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/2?curriculum_id=506324b3a7dffd00020bf661

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661

我已为第6步编写了以下代码,但它错误地给了我以下错误

Oops, try again. Your code returned 'paper wins' instead of 'rock wins' when the inputs are rock and scissors

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock") {

        if (choice2 === "scrissors") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
}
};
console.log(compare('rock','scrissors'));
console.log(compare('rock','rock'));
console.log(compare('scissor','scissor'));
console.log(compare('paper','paper'));
console.log(compare('rock','paper'))

并且在步骤7中将其接受为正确答案。

它出了什么问题?

更新代码:

var compare = function(choice1, choice2) {
if(choice1 === choice2) {
return "The result is a tie!";
}
else if(choice1 === "rock") {

        if (choice2 === "scrissors") {
            return "rock wins";
        }
        else {
            return "paper wins";
        }
}
};
console.log(compare('rock','scrissors'));
console.log(compare('rock','rock'));
console.log(compare('rock','paper'))

3 个答案:

答案 0 :(得分:1)

更新:确保你拼写:剪刀不是scrissors。 CodeAcademy将使用内部验证工具来检查您的结果,如果您没有正确拼写,该工具将失败,因为您没有处理他们已经告诉您要处理的输入!

拼写错误:

if (choice2 === "scrissors") { ... }

console.log(compare('rock','scrissors'));
console.log(compare('scissor','scissor'));

答案 1 :(得分:1)

试试这段代码。

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");
    }
}

};

您发布的程序拼写错误就行了 if(choice2 ===“scrissors”){...}

尝试将其更改为剪刀。

答案 2 :(得分:1)

以下是如何使其发挥作用的示例:

<h1 id="title">Javascript  "Rock, Paper, Scrissors"</h1>
<select id="choice1">
  <option selected value="base">Please Select</option>
  <option value="rock">Rock</option>
  <option value="paper">Paper</option>
  <option value="scissors">Scissors</option>
</select>
<select id="choice2">
  <option selected value="base">Please Select</option>
  <option value="rock">Rock</option>
  <option value="paper">Paper</option>
  <option value="scissors">Scissors</option>
</select>
<input type="submit" id="byBtn" value="Compare" onclick="compare()"/>


function compare(){
   var choice1 = document.getElementById('choice1').value;
   var choice2 = document.getElementById('choice2').value;
   if(choice1==='base' || choice2==='base')
   {
       alert('Select an option for both inputs!');
       return;
   }
   if(choice1 === choice2) {
    alert("The result is a tie!");
    }
    else if(choice1 === "rock") {

            if (choice2 === "scissors") {
                alert("Rock wins");
            }
            else {
                alert("Paper wins");
            }
    }
    else if(choice1 === "paper") {

            if (choice2 === "scissors") {
                alert("Scissors wins");
            }
            else {
                alert("Paper wins");
            }
    }
    else if(choice1 === "scissors") {

            if (choice2 === "rock") {
                alert("Rock wins");
            }
            else {
                alert("Scissors wins");
            }
    }
}

Neil告诉你这是一个tippo的错误。我在Fiddle中做了一个例子,你可以检查它并按照你喜欢的方式使用它。

Nelson Parra。

Example rock, paper and scissors