Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现输出一个十六进制的随机颜色(#af0128a) #170

Open
lgwebdream opened this issue Jul 6, 2020 · 5 comments
Open

实现输出一个十六进制的随机颜色(#af0128a) #170

lgwebdream opened this issue Jul 6, 2020 · 5 comments
Labels
JavaScript teach_tag 快手 company

Comments

@lgwebdream
Copy link
Owner

No description provided.

@lgwebdream lgwebdream added JavaScript teach_tag 快手 company labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

@yulishuta
Copy link

function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function getColor() {
    let result = new Array(6)
    let i = 0
    let hexMap = ['a', 'b', 'c', 'd', 'e', 'f']
    while (i < 6) {
        let data = getRandomInt(0, 16)
        result[i] = data > 10 ? hexMap[data % 10] : data

        i++
    }
    return `#${result.join('')}`
}

@qzruncode
Copy link

function randomColor() {
    const r = (Math.floor(Math.random() * 255)).toString(16);
    const g = (Math.floor(Math.random() * 255)).toString(16);
    const b = (Math.floor(Math.random() * 255)).toString(16);
    const a = (Math.random()).toString(16).slice(2, 4);
    console.log(a);
    return `#` + r + g + b + a;
  }

@dty999
Copy link

dty999 commented Jul 7, 2021

// 实现输出一个十六进制的随机颜色(#af0128a)
let color = '#' + parseInt(Math.random() * 0x1000000).toString(16).padStart(6, '0')
console.log(color);

@ZhuJingLe
Copy link

var colorStr = '#';
function randomColor() {
    var items = [0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'];
    var index = Math.floor(Math.random()*15);
    return items[index];
} 
for (let i = 0; i < 6; i++) {
    colorStr += randomColor();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 快手 company
Projects
None yet
Development

No branches or pull requests

5 participants