错误-“未捕获的TypeError:无法读取未定义的属性'length”?”

时间:2020-02-17 23:24:44

标签: javascript

我目前正在学习Javascript,他们希望我构建一个Rock, paper and scissors game

这是我到目前为止的代码:

let computerPlay = (array) => {
  return array[Math.floor(Math.random() * 
  array.length)]
}

const array = (["Rock", "Paper", "Scissors"])
console.log(computerPlay(array))

let singleRound = (playerSelection, 
computerSelection) => {

if ( playerSelection == "rock" || playerSelection 
== "Paper" || 
playerSelection == "scissors") {
  computerSelection = computerPlay
} else if (playerSelection == null) {
  "Canceled"
}



}

let playerSelection = prompt("What do you 
select?")
const computerSelection = computerPlay()

谢谢

1 个答案:

答案 0 :(得分:2)

功能定义:

let computerPlay = (array) => { //... }

调用函数:

记住computerPlay时需要一个array作为参数。

computerPlay(YOUR_ARRAY_HERE);