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