我如何 return 或 console.log 我的函数到 return 我选择的数组?

How do I return or console.log my function to return my array of choice?

我正在练习我的 JavaScript 我只是想做一个简单的剪刀石头布游戏,我遵循了一个小指南,但是当我 console.log 它时,它只是打印函数而不是选择的数组。我如何获得这些选择?

let choice = ['Rock', 'Paper', 'Scissors'];

function computerPlay () {
    return choice[Math.floor(Math.random * choice.length)];
}

console.log(computerPlay);

您需要对代码进行 2 处更改以 运行

  1. Math.random改为Math.random()
  2. console.log(computerPlay)更改为console.log(computerPlay())

You need to call a function using fn_name() to execute it.

let choice = ['Rock', 'Paper', 'Scissors'];

function computerPlay() {
  return choice[Math.floor(Math.random() * choice.length)];
}

console.log(computerPlay());