编写一个函数,返回数组中任意数量的十六进制颜色

问题描述 投票:0回答:2

我堆栈,我不知道如何创建十六进制颜色的数组。如果您知道,请分享。

function concatArray(n){
let arr = []
let hashtag = '#'
let semicolon = ','
let r = Math.floor(Math.random() * 16)
let g = Math.floor(Math.random() * 16)
let b = Math.floor(Math.random() * 16)
  for(i = 0; i < n; i++){
  arr.push(hashtag,r,g,b,semicolon)
  }
   console.log(arr.join(''))
  }
arrays colors hex
2个回答
0
投票
    let arrayOfHexaColors = () => {
  let allHexCodes = '123456789abcdef';
  let loopCount = parseInt(Math.random() * 50);
  let hexColorArr = [];
  
  for(let x=1; x<=loopCount; x++) {
     let hexChars = [];
     for (let i = 0; i < 6; i++) {
        hexChars.push(allHexCodes[parseInt(Math.random() * allHexCodes.length) - 1]);
     }
     hexColorArr.push('#' + hexChars.join(''));
  }
  return hexColorArr;
} 
console.log(arrayOfHexaColors());

0
投票

函数 arrayOfhexaColors(){ 让 alpha = 'abcdef123456789'

let randomloop = Math.floor(Math.random()*10)
let hexcar = [];
for(let i =0 ;i<randomloop;i++){
    let hash = '#'
    for(let j =0;j<6;j++){
        hash+=alph[Math.floor(Math.random()*alph.length)]
    }
    hexcar.push(hash)
}
console.log(hexcar);

}

arrayOfhexaColors()

© www.soinside.com 2019 - 2024. All rights reserved.