Native JS code implementation randomly generates a hexadecimal color value


Encapsulate a function

function getColor() {
      var str = "#";
      //An array of hexadecimal values
      var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
      for (var i = 0; i < 6; i++) {
        //Each random number generated is an index, which finds the corresponding values in the array and splices them together
        var num = parseInt(Math.random() * 16);
        str += arr[num];
      return str;

Call getcolor() to randomly get a hexadecimal color value

Recommended Today

Zhu Hui: code principle of Linux kernel iowait time

This article is reprinted and the copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source. Author: Zhu Hui (tea) Source: WeChat official account Linux yard (id: linuxdev) Author introduction Zhu Hui, personal homepage, WeChat official account cschatcs. I have been a simulator […]