Open triboby opened 5 years ago
// 生成不同类型的颜色
var sixArr = new Array(6).fill(0) console.log(sixArr) var hundredColor = [] function produceColor() { let colorVal = '#' sixArr.forEach(() => { colorVal += Math.floor(Math.random() * 16).toString(16) }) return colorVal } do { let newColorVal = produceColor() let index = hundredColor.findIndex(() => { return === newColorVal }) if (index === -1) { hundredColor.push(newColorVal) } } while (hundredColor.length < 101) console.log(hundredColor)
let colorList = new Set();
function getRandomColor() { let color = parseInt(Math.random() * (16777215 + 1), 10).toString(16); return "#" + color.padStart(6, "0"); } while (true) { colorList.add(getRandomColor()); if (colorList.size >= 10) { break; } } console.log(colorList);
// 生成不同类型的颜色
var sixArr = new Array(6).fill(0) console.log(sixArr) var hundredColor = [] function produceColor() { let colorVal = '#' sixArr.forEach(() => { colorVal += Math.floor(Math.random() * 16).toString(16) }) return colorVal } do { let newColorVal = produceColor() let index = hundredColor.findIndex(() => { return === newColorVal }) if (index === -1) { hundredColor.push(newColorVal) } } while (hundredColor.length < 101) console.log(hundredColor)
遍历比较了
今天碰到个面试题 如何通过十六进制,每次随机生成一个不同的背景色,要求生成100个不重复,且不遍历比较