【发布时间】:2022-06-10 17:36:16
【问题描述】:
一个非常愚蠢的问题,但我刚刚开始学习 javascript,需要编写代码让计算机随机选择数组中的三个值中的一个,如下所示:
function computerPlay() {
let choices = ['rock', 'paper', 'scissors'];
let computerChoice = Math.floor(Math.random() * choices.length);
return choices[computerChoice];
}
我的问题是为什么在返回选择[computerChoice] 期间需要方括号?它是如何工作的?这叫什么?
【问题讨论】:
-
它通过索引访问数组的一个特定元素