当前位置:网站首页>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));
边栏推荐
- Jenkins安装
- TIPC messaging3
- Use of vscode tool
- Verilog 和VHDL有符号数和无符号数相关运算
- Openmldb meetup No.4 meeting minutes
- Calculate the sum of sequences
- How to use ide to automatically sign and debug Hongmeng application
- Luogu p1892 [boi2003] Gang (and search for variant anti set)
- [AGC] build service 3 - authentication service example
- [quick application] win7 system cannot run and debug projects using Huawei ide
猜你喜欢
三.芯片启动和时钟系统
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Tick Data and Resampling
MTK full dump抓取
Verilog and VHDL signed and unsigned number correlation operations
What are the software product management systems? Inventory of 12 best product management tools
[play with FPGA learning 2 in simple terms ----- design skills (basic grammar)]
Matlab processing of distance measurement of experimental electron microscope
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
V2x SIM dataset (Shanghai Jiaotong University & New York University)
随机推荐
flink二开,实现了个 batch lookup join(附源码)
二叉树专题--AcWing 1589. 构建二叉搜索树
Logu p3398 hamster looks for sugar (double LCA on the tree to judge whether the two paths in the tree intersect)
Use of vscode tool
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Overview of integrated learning
TIPC Getting Started6
如何用list组件实现tabbar标题栏
从攻击面视角,看信创零信任方案实践
[AGC] build service 3 - authentication service example
Calculate the sum of sequences
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
C# 文件与文件夹操作
力扣(LeetCode)182. 查找重复的电子邮箱(2022.07.01)
spritejs
Implementation of six singleton modes
What are the software product management systems? Inventory of 12 best product management tools
PCL point cloud to depth image
Tick Data and Resampling
webauthn——官方开发文档