How to change just the div background-color and not the entirely body-color?


Currently bstyle is assigned, so when you write:

bstyle.backgroundColor = "green";

you are changing the background of the body and not the hello div.

Another way to reduce repetition in your code would be to do the following. Only click on the blurred section if you want to see a working solution.

function updateBackground() {
    hr = (new Date()).getHours(),
    hello = document.querySelector(".hello");  
  var divBg = hr < 10 
    ? ['yellow', 'black', 'Have a good morning']
    : hr < 20
      ? ['green', 'white', 'Have a good day']
      : ['black', 'white', 'Have a good night']
  var [bg, color, text] = divBg; = bg; = color;
  hello.innerText = text; 

setInterval(updateBackground, 1000 * 60);