在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 官宣新年祝福语简短