From c1fc6ba5a926ffe3ebba6ee2b1b715bbb38aa383 Mon Sep 17 00:00:00 2001 From: Mohammad Atallah Date: Fri, 6 Aug 2021 07:09:26 +0300 Subject: [PATCH] Generate random background with a dynamic wide range --- js/script.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/js/script.js b/js/script.js index 803c165..9cc23b3 100644 --- a/js/script.js +++ b/js/script.js @@ -1,12 +1,11 @@ //Choose a random color -const button = document.querySelector('button') -const body = document.querySelector('body') -const colors = ['red', 'green', 'blue', 'yellow', 'pink', 'purple'] +const button = document.querySelector("button"); +const body = document.querySelector("body"); -body.style.backgroundColor = 'violet' -button.addEventListener('click', changeBackground) +changeBackground(); +button.addEventListener("click", changeBackground); -function changeBackground(){ -const colorIndex= parseInt(Math.random()*colors.length) -body.style.backgroundColor = colors[colorIndex] +function changeBackground() { + const h = parseInt(Math.random() * 360); + body.style.backgroundColor = `hsl(${h}, 60%, 45%)`; }