0
点赞
收藏
分享

微信扫一扫

生成任意个任意两个数之间的随机数的数组,并统计每个数出现的次数

花明 2022-02-01 阅读 45
javascript
function random(min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + 1
        }
        let arrRandom = [];
        for (let i = 0; i < 1000; i++) {
            arrRandom.push(random(1, 5))
        }
        console.log(arrRandom.sort());
        let countObj = {};
        for (i = 0; i <= arrRandom.length - 1; i++) {
            let v = arrRandom[i];
            if (countObj[v]) {
                countObj[v]++;
            } else {
                countObj[v] = 1;
            }
        }
        console.log(countObj); //{ '2': 2, '3': 1, '5': 4, '11': 2 }
举报

相关推荐

两个数组的交集

0 条评论