From 33fbb9a989fa7c0fc5de6daeabbdb5ed354a9774 Mon Sep 17 00:00:00 2001 From: alizain10 <71589343+alizain10@users.noreply.github.com> Date: Fri, 25 Dec 2020 20:26:59 +0500 Subject: [PATCH] Added all colors The previous code had limited choice of colors. I have structured it in a way where you can access all the colors! --- js/script.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/js/script.js b/js/script.js index 803c165..07b35c5 100644 --- a/js/script.js +++ b/js/script.js @@ -1,12 +1,17 @@ //Choose a random color const button = document.querySelector('button') const body = document.querySelector('body') -const colors = ['red', 'green', 'blue', 'yellow', 'pink', 'purple'] +const hexArr = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'] body.style.backgroundColor = 'violet' button.addEventListener('click', changeBackground) function changeBackground(){ -const colorIndex= parseInt(Math.random()*colors.length) -body.style.backgroundColor = colors[colorIndex] +var hash = '#' +for (let i = 0; i < 6; i++){ +let count = Math.random() * hexArr.length +count = Math.floor(count) +hash += hexArr[count] +} +body.style.backgroundColor = hash }