I am trying to create a color mixer where when you click on the first two divs, they change colors. The combination of the colors of the first two divs are supposed to make the third div a mixture of the first two. So if the first two divs are red then the third should also be red. It is not working and the console is saying Uncaught TypeError: Cannot read property 'backgroundColor' of undefined.
HTML
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {
width: 100px;
height: 100px;
background-color: #00DD00;
margin: 5px;
float: left;
}
</style>
</head>
<body>
<script src="js/main.js"></script>
</body>
</html>
Here is my JS
for(var i = 0; i < 1; i++) {
var newDiv = document.createElement("div");
newDiv.addEventListener('click', changeColor);
document.body.appendChild(newDiv);
var newDivTwo = document.createElement("div");
newDivTwo.addEventListener('click', changeColor);
document.body.appendChild(newDivTwo);
var newDivThree = document.createElement("div");
newDivThree.addEventListener('click', changeColor);
document.body.appendChild(newDivThree);
}
function changeColor(newDiv, newDivTwo, newDivThree){
if (newDiv.style.backgroundColor =='rgb(255, 0, 0)' && newDivTwo.style.backgroundColor =='rgb(255, 0, 0)'){
newDivThree.style.backgroundColor ='rgb(255, 0, 0)';
}
else if (newDiv.style.backgroundColor =='rgb(255, 0, 0)' && newDivTwo.style.backgroundColor=='rgb(0, 0, 255)'){
newDivThree.style.backgroundColor='rgb(255, 0, 255)';
}
}