当前位置:网站首页>JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
JS——每次调用从数组里面随机取一个数,且不能与上一次为同一个
2022-07-02 09:32:00 【月来better】
const arr = [1, 2, 3, 4, 5];
let ranArr = [];
const randomchoice = function factory(arr) {
let rnd = Math.floor(Math.random() * arr.length);
if (ranArr.includes(rnd)) return factory(arr);
ranArr.push(rnd);
return arr[rnd];
};
console.log(randomchoice(arr));
console.log(randomchoice(arr));
console.log(randomchoice(arr));
console.log(randomchoice(arr));
console.log(randomchoice(arr));
边栏推荐
- From the perspective of attack surface, see the practice of zero trust scheme of Xinchuang
- 2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
- js中给数组添加元素的方法有哪些
- Overview of integrated learning
- 如何用list组件实现tabbar标题栏
- ros gazebo相关包的安装
- 二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
- Verilog and VHDL signed and unsigned number correlation operations
- 函数式接口和方法引用
- Huawei game failed to initialize init with error code 907135000
猜你喜欢
ctf 记录
PKG package manager usage instance in FreeBSD
TIPC messaging3
[play with FPGA learning 4 in simple terms ----- talk about state machine design]
JVM之垃圾回收器
ImportError: cannot import name ‘Digraph‘ from ‘graphviz‘
flink二開,實現了個 batch lookup join(附源碼)
TIPC Service and Topology Tracking4
tqdm的多行显示与单行显示
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
随机推荐
三.芯片启动和时钟系统
华为快应用中如何实现同时传递事件对象和自定义参数
TIPC Service and Topology Tracking4
static 函数中的静态变量
Jenkins安装
QT学习日记7——QMainWindow
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
How to transfer event objects and user-defined parameters simultaneously in Huawei express applications
二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
PCL projection point cloud
How does the whole network display IP ownership?
OpenMLDB Meetup No.4 会议纪要
[play with FPGA learning 5 in simple terms ----- reset design]
二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
洛谷 P3398 仓鼠找 sugar(树上倍增 lca 判断树中两条路径是否相交 结论)
每月1号开始计算当月工作日
Iii. Système de démarrage et d'horloge à puce
How to use ide to automatically sign and debug Hongmeng application
Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
Tick Data and Resampling