From ca831caefbd89938a5f7a7bf5f00306d0d3e6dad Mon Sep 17 00:00:00 2001 From: Oscar Shrimpton Date: Mon, 29 Jan 2018 19:36:46 +0000 Subject: [PATCH 1/3] Fixed randomHexColorCode() sometimes giving a string shorter than 7 characters. (Fixes #580) --- snippets/randomHexColorCode.md | 4 ++-- test/randomHexColorCode/randomHexColorCode.test.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/randomHexColorCode.md b/snippets/randomHexColorCode.md index b8567aa9a..61231383c 100644 --- a/snippets/randomHexColorCode.md +++ b/snippets/randomHexColorCode.md @@ -6,8 +6,8 @@ Use `Math.random` to generate a random 24-bit(6x4bits) hexadecimal number. Use b ```js const randomHexColorCode = () => { - let n = ((Math.random() * 0xfffff) | 0).toString(16); - return '#' + (n.length !== 6 ? ((Math.random() * 0xf) | 0).toString(16) + n : n); + let n = (((Math.random() * 0xfffff) | 0) << 6).toString(16); + return '#' + n.slice(0, 6); }; ``` diff --git a/test/randomHexColorCode/randomHexColorCode.test.js b/test/randomHexColorCode/randomHexColorCode.test.js index 63f117367..eb2cdba9f 100644 --- a/test/randomHexColorCode/randomHexColorCode.test.js +++ b/test/randomHexColorCode/randomHexColorCode.test.js @@ -6,7 +6,7 @@ test('Testing randomHexColorCode', (t) => { //Please go to https://github.com/substack/tape t.true(typeof randomHexColorCode === 'function', 'randomHexColorCode is a Function'); //t.deepEqual(randomHexColorCode(args..), 'Expected'); - //t.equal(randomHexColorCode(args..), 'Expected'); + t.equal(randomHexColorCode().length, 7); //t.false(randomHexColorCode(args..), 'Expected'); //t.throws(randomHexColorCode(args..), 'Expected'); t.end(); From d6cf9bddf4f707f932fc55e3445c12409a5b1b25 Mon Sep 17 00:00:00 2001 From: Oscar Shrimpton Date: Mon, 29 Jan 2018 19:39:33 +0000 Subject: [PATCH 2/3] Removed unnecessary OR in randomHexColorCode() --- snippets/randomHexColorCode.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/randomHexColorCode.md b/snippets/randomHexColorCode.md index 61231383c..84b4dfb64 100644 --- a/snippets/randomHexColorCode.md +++ b/snippets/randomHexColorCode.md @@ -6,7 +6,7 @@ Use `Math.random` to generate a random 24-bit(6x4bits) hexadecimal number. Use b ```js const randomHexColorCode = () => { - let n = (((Math.random() * 0xfffff) | 0) << 6).toString(16); + let n = (((Math.random() * 0xfffff)) << 6).toString(16); return '#' + n.slice(0, 6); }; ``` From 50fddfbbe63abef442e5771b49bb194bada2f05a Mon Sep 17 00:00:00 2001 From: Oscar Shrimpton Date: Mon, 29 Jan 2018 19:43:25 +0000 Subject: [PATCH 3/3] Times by 1000000 instead of shifting. --- snippets/randomHexColorCode.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/randomHexColorCode.md b/snippets/randomHexColorCode.md index 84b4dfb64..3a06f06ea 100644 --- a/snippets/randomHexColorCode.md +++ b/snippets/randomHexColorCode.md @@ -6,7 +6,7 @@ Use `Math.random` to generate a random 24-bit(6x4bits) hexadecimal number. Use b ```js const randomHexColorCode = () => { - let n = (((Math.random() * 0xfffff)) << 6).toString(16); + let n = (((Math.random() * 0xfffff)) * 1000000).toString(16); return '#' + n.slice(0, 6); }; ```