使用之间有什么区别:
function random(array){
return array[Math.floor(Math.random()*array.length)];
}
(对比)
function random(array){
return Math.floor(Math.random()*array.length);
}
为什么我们要在前面添加return数组[]??
我通过 MDN 示例中的Silly Story Generator https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Silly_story_generator
最佳答案
这只是返回 0 到 array.length - 1 之间的随机数
function random(array) {
return Math.floor(Math.random() * array.length);
}
console.log(random([50,30,100]));
这是通过使用随机数 the index 来获取数组中的随机元素。
function random(array) {
return array[Math.floor(Math.random() * array.length)];
}
console.log(random([50,30,100]));
关于javascript - 关于 Javascript 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59463000/