简体   繁体   English

为什么将 function 参数与来自 function 的字符串进行比较时返回“未定义”?

[英]Why is 'undefined' returned when comparing an function argument to a string from a function?

I created cpuPlay to call either rock, paper, or scissors, whenever a random number between 0-2 is called.我创建了 cpuPlay 来调用石头、布或剪刀,只要调用 0-2 之间的随机数。 This function works properly, but rps() is the function that I can't seem to build properly after 3 days of doing this.这个 function 可以正常工作,但是rps()是 function,我在执行此操作 3 天后似乎无法正确构建。 No matter how I build this, it seems I keep on getting one of four answers when I call rps().无论我如何构建它,当我调用 rps() 时,我似乎总是得到四个答案之一。 The answers I get are either 'rock','paper','scissors','undefined' .我得到的答案是'rock','paper','scissors','undefined'

Before I thought it was because I used.toLowerCase() on playerChoice, but either way, this function has been giving the same results.之前我认为这是因为我在 playerChoice 上使用了.toLowerCase(),但不管怎样,这个 function 一直给出相同的结果。 If you keep on running rps('paper'), it eventually shows.如果你继续运行 rps('paper'),它最终会显示出来。 I'm not sure how I should go any further with this problem, or maybe I'm not supposed to be able to compare.我不确定我应该如何 go 进一步解决这个问题,或者我不应该能够进行比较。 I don't even know anymore.我什至不知道了。

function cpuPlay(){
    let numberGen = Math.floor(Math.random() * 3)

  if (numberGen === 0){
    return 'rock'
  }
  if (numberGen === 1){
    return 'paper'
  }
  if (numberGen === 2){
    return 'scissors'
  }
}
// cpuPlay()

function rps(playersChoice){
  if(playersChoice == 'paper' && cpuPlay() == 'rock'){
    console.log('rock')
  } else if (playersChoice == 'paper' && cpuPlay() == 'paper'){
    console.log('paper')
  } else if (playersChoice == 'paper' &&  cpuPlay() == 'scissors'){
    console.log('scissors');
  }
}
rps('paper')

You are calling cpuPlay() in each expression so it is possible that every conditional fails, because cpuPlay() value is changing, you can try this:你在每个表达式中调用cpuPlay()所以每个条件都可能失败,因为cpuPlay()值正在改变,你可以试试这个:

 function cpuPlay(){ let numberGen = Math.floor(Math.random() * 3) if (numberGen === 0){ return 'rock' } if (numberGen === 1){ return 'paper' } if (numberGen === 2){ return 'scissors' } } function rps(playersChoice){ let currentCpuPlay = cpuPlay() if(playersChoice == 'paper' && currentCpuPlay == 'rock'){ console.log('rock') } else if (playersChoice == 'paper' && currentCpuPlay == 'paper'){ console.log('paper') } else if (playersChoice == 'paper' && currentCpuPlay == 'scissors'){ console.log('scissors'); } } rps('paper')

cpuPlay() generates a random value every time you call it .每次调用cpuPlay()时都会生成一个随机值。

You are generating a random value and comparing it to rock .您正在生成一个随机值并将其与rock进行比较。 Then you generate a new random value and compare it to paper .然后生成一个新的随机值并将其与paper进行比较。 Then you generate a third random value and compare it to scissors .然后生成第三个随机值并将其与scissors进行比较。

You should generate the CPU player's response once , at the top of the comparison function and then compare that value each time.您应该在比较 function 的顶部生成一次CPU 播放器的响应,然后每次都比较该值。

function rps(playersChoice){
    const cpuChoice = cpuPlay();
    if(playersChoice == 'paper' && cpuChoice == 'rock'){

the cpuPlay() is returning random results at every if statement cpuPlay()在每个 if 语句中返回随机结果

so when in the first if and the result is not rock, it will jump to next statement again, then but at the second if , the cpuPlay() is returning different results, so end up your function has a chance to have no results.因此,当在第一个if中结果不是 rock 时,它会再次跳转到下一个语句,然后在第二个if中, cpuPlay()返回不同的结果,因此最终您的 function 有机会没有结果。

function rps(playersChoice){
  let cpuRoll = cpuPlay(); // only random once per game
  if(playersChoice == 'paper' && cpuRoll == 'rock'){
    console.log('rock')
  } else if (playersChoice == 'paper' && cpuRoll == 'paper'){
    console.log('paper')
  } else if (playersChoice == 'paper' &&  cpuRoll == 'scissors'){
    console.log('scissors');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM