Travis build: 254
This commit is contained in:
@ -1440,12 +1440,8 @@ Omit the second argument to use the default regex.</p>
|
||||
</code></pre>
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="randomhexcolorcode">randomHexColorCode</h3></div><div class="section double-padded">
|
||||
<p>Generates a random hexadecimal color code.</p>
|
||||
<p>Use <code>Math.random</code> to generate a random number and limit that number to fall in between 0 and 16 using <code>Math.floor</code>. Use the generated random number as index to access a character from 0 to F. Append it to <code>color</code> till the length is not <code>7</code>.</p>
|
||||
<pre><code class="language-js">const randomHexColorCode = () => {
|
||||
let color = '#';
|
||||
while(color.length < 7) color += '0123456789ABCDEF'[Math.floor(Math.random() * 16)];
|
||||
return color;
|
||||
}
|
||||
<p>Use <code>Math.random</code> to generate a random 24-bit(6x4bits) hexadecimal number. Use bit shifting and then convert it to an hexadecimal String using <code>toString(16)</code>.</p>
|
||||
<pre><code class="language-js">const randomHexColorCode = () => '#'+(Math.random()*0xFFFFFF<<0).toString(16);
|
||||
// randomHexColorCode() -> "#e34155"
|
||||
// randomHexColorCode() -> "#fd73a6"
|
||||
// randomHexColorCode() -> "#4144c6"
|
||||
|
||||
Reference in New Issue
Block a user