From d5075f8a580b9f243693e2edaf8d491792f57b3e Mon Sep 17 00:00:00 2001 From: "jason.zhu" Date: Mon, 17 May 2021 16:36:42 +1000 Subject: [PATCH] Finished Hex page of Color Flipper, Color Flipper has all required feature completed --- 1-color-flipper/hex.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/1-color-flipper/hex.js b/1-color-flipper/hex.js index 88aba81..e88af9b 100644 --- a/1-color-flipper/hex.js +++ b/1-color-flipper/hex.js @@ -1 +1,17 @@ const hex = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"]; +// #f15025 orange color +const btn = document.getElementById('btn'); +const color = document.querySelector(".color"); + +btn.addEventListener('click', function(){ + let hexColor = '#'; + for (let i = 0; i < 6; i++) { + hexColor += hex[getRandomNumber()]; + } + color.textContent = hexColor; + document.body.style.backgroundColor = hexColor; +}); + +function getRandomNumber() { + return Math.floor(Math.random() * hex.length); +} \ No newline at end of file