Rock Paper Scissors javascript函数不显示结果

时间:2018-02-05 07:34:57

标签: javascript function

所以,我试图让这个石头剪刀游戏工作,但它只显示"其他东西"尽管两个选秀权相等(它应该显示"它是一个平局!")。警告。

此错误也会出现在第21行的控制台上:NS_ERROR_XPC_SECURITY_MANAGER_VETO:

下面是代码,并非所有选项都是完整的(纸上书写等)这只是一个测试:

<script>

function compPlay (){
  let comChoice= Math.random();
  console.log(comChoice);
  if (comChoice<=0.33){
    comChoice== "rock";
  } else if (comChoice<=0.66){
    comChoice== "scissors";
  } else {
    comChoice== "paper";
  }
  return;
}

function userPlay (){
  prompt("rock, paper or scissors");
  return prompt;
  }

function thegame (compPlay,userPlay){
  if (compPlay=="rock" && userPlay=="rock") {
    alert("its tied");
  } else {
    alert("something else");
  }
}

compPlay();
userPlay();
thegame();

</script>

4 个答案:

答案 0 :(得分:1)

您没有从compPlay返回comChoice。如果你返回comChoice,它应该可以工作!

return;应替换为return comChoice;

答案 1 :(得分:1)

您的功能不会返回comChoice。它应该是这样的。

function compPlay (){
  let comChoice= Math.random();
  console.log(comChoice);
  if (comChoice<=0.33){
    comChoice== "rock";
  } else if (comChoice<=0.66){
    comChoice== "scissors";
  } else {
    comChoice== "paper";
  }
  return comChoice;
}

function userPlay (){
  var promData = prompt("rock, paper or scissors");
  return promData;
}

function thegame (compPlay,userPlay){
    if (compPlay=="rock" && userPlay=="rock") {
        alert("its tied");
    } else {
        alert("something else");
    }
}

var cChoice = compPlay();
var uChoice = userPlay();
thegame(cChoice,uChoice);

然后你应该使用comChoice检查它是否匹配。

答案 2 :(得分:1)

你的代码中存在很多错误,尝试在你的控制台上运行它,测试工作;

function compPlay() {
    let comChoice = Math.random();
    console.log(comChoice);
    if (comChoice <= 0.33) {
        comChoice = "rock";
    } else if (comChoice <= 0.66) {
        comChoice = "scissors";
    } else {
        comChoice = "paper";
    }
    return comChoice;
}


function userPlay() {
    let promData = prompt("rock, paper or scissors");
    thegame(compPlay(), promData);
}

function thegame(comPlayData, promData) {
    if (comPlayData === promData) {
        console.log(comPlayData, promData);
        alert("its tied");
    } else {
        alert("you:" + promData + ',me:' + comPlayData);
    }
}

userPlay();

答案 3 :(得分:0)

如前所述,所提供的代码中存在相当多的错误。

首先:在函数compPlay中赋值:你只需要一个'='

第二:函数comPlay不返回请求的值comChoice

第三:最后你只需要调用带有上述两个函数的第三个函数作为参数。使用相应的日志检查下面的代码,看看是否有帮助

function compPlay (){

let comChoice= Math.random();
  console.log(comChoice);
  if (comChoice<=0.33){
    comChoice= "rock";
  } else if (comChoice<=0.66){
    comChoice= "scissors";
  } else {
    comChoice= "paper";
  }
  return comChoice;
  }

  function userPlay (){
  var input = prompt("rock, paper or scissors");
  console.log(input);
  return input;
  }

  function thegame (compPlay,userPlay){
  if (compPlay==userPlay) {
    alert("its tied");
  } else {
    alert("something else");
  }
  console.log('compPlay: '+compPlay);
  console.log('userPlay: '+userPlay);
  }

  // compPlay();
  // userPlay();
  thegame(compPlay(),userPlay());