返回范围内的5个随机数,第一个数字不能为零-JavaScript
我们需要编写一个JavaScript函数,该函数生成一个正好由五个唯一随机数组成的数组。条件是所有数字都必须在[0,9]范围内,并且第一个数字不能为0。
示例
以下是代码-
const fiveRandoms = () => {
const arr = []
while (arr.length < 5) {
const random = Math.floor(Math.random() * 10);
if (arr.indexOf(random) > -1){
continue;
};
if(!arr.length && !random){
continue;
}
arr[arr.length] = random;
}
return arr;
};
console.log(fiveRandoms());输出结果
这将在控制台中产生以下输出-
[ 9, 0, 8, 5, 4 ]
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短