From 7bf82d879a4c0cb7d0f6330829edbdaf24cc17a5 Mon Sep 17 00:00:00 2001 From: LORDBABUINO Date: Mon, 2 Dec 2019 16:12:28 -0300 Subject: [PATCH 1/2] Fix setColor function --- script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script.js b/script.js index ffb9182..caa1116 100644 --- a/script.js +++ b/script.js @@ -43,7 +43,7 @@ function randomize(arr) { } function setColor(id) { - id.style.setProperty('--color', randomize(colors)); + id.style.backgroundColor = randomize(colors); } function populate(element) { From 2149b29cad7889fdb20641aea7712dcfdefb84a5 Mon Sep 17 00:00:00 2001 From: LORDBABUINO Date: Mon, 2 Dec 2019 16:41:09 -0300 Subject: [PATCH 2/2] Fix randomization --- script.js | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/script.js b/script.js index caa1116..be4c41c 100644 --- a/script.js +++ b/script.js @@ -34,21 +34,31 @@ const colors = [ const elementWord = document.querySelectorAll(".word"); -function randomize(arr) { - const item = Math.floor(Math.random() * arr.length - 5); - const value = arr.splice(item, 1); - arr.push(value); +function randomize(arr, last) { + const filteredArr = arr.filter((value) => value !== last) + const item = Math.floor(Math.random() * filteredArr.length); - return value; + return filteredArr[item]; +} + +function intToHex(c) { + const hex = c.toString(16).toUpperCase(); + return hex.length == 1 ? "0" + hex : hex; +} + +function rgbToHex(rgb) { + const arr = eval('[' + rgb.slice(4,-1) + ']') + return "#" + intToHex(arr[0]) + intToHex(arr[1]) + intToHex(arr[2]); } function setColor(id) { - id.style.backgroundColor = randomize(colors); + const currentColor = rgbToHex(getComputedStyle(id).backgroundColor) + id.style.backgroundColor = randomize(colors, currentColor); } function populate(element) { setColor(element); - element.innerHTML = randomize(sources[element.id]); + element.textContent = randomize(sources[element.id], element.textContent); } function setChange() {