在JavaScript中生成随机十六进制颜色
我们需要编写一个JavaScript函数randomColor,该函数每次调用时都会返回随机生成的十六进制颜色。
因此,让我们为该函数编写代码-
示例
为此的代码将是-
const randomColor = () => {
let color = '#';
for (let i = 0; i < 6; i++){
const random = Math.random();
const bit = (random * 16) | 0;
color += (bit).toString(16);
};
return color;
};
console.log(randomColor());
console.log(randomColor());
console.log(randomColor());
console.log(randomColor());
console.log(randomColor());
console.log(randomColor());
console.log(randomColor());输出结果
控制台中的输出将为-
#762b46 #cfa0bf #a20ee1 #c2f7e0 #5d5822 #380f30 #805408
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短