Random Color Div

by Subigya Panta

HTML

<div id="container">

</div>
<button onclick="changeBgColor('red')">Red</button>
<button onclick="changeBgColor('green')">Green</button>
<button onclick="changeBgColor('blue')">Blue</button>
<button onclick="random()">Random</button>

CSS

#container {
  width:100px;
  height: 100px;
  border: 1px solid black;
}

JavaScript

function changeBgColor(color_name) {
	
	if(color_name=='red') { document.getElementById('container').style.backgroundColor="rgb(255,0,0)";
  }
	else if(color_name=='green') { document.getElementById('container').style.backgroundColor="rgb(0,255,0)";
  }
	else if(color_name=='blue') { document.getElementById('container').style.backgroundColor="rgb(0,0,255)";
  }
}

function getColor(){
	var colors = ['red', 'green', 'blue'];
	var num = Math.random();
	var requiredNum = num*3;
  console.log(Math.floor(requiredNum));
	return colors[Math.floor(requiredNum)];
}
 function random(){
 		var color = getColor();
    changeBgColor(color);
 }