I have created a function addElements() which changes an image, background color of the body and text color of a p tag. Now when I run the following code, the image changes at the setInterval infinetely but the colors do not change even once.
function addElements() {
var img = document.getElementById('img');
var image = images[Math.floor(Math.random() * images.length)];
img.src = image;
var name = document.getElementById('name');
var color = colors = colors[Math.floor(Math.random() * colors.length)];
document.body.style.backgroundColor = color;
name.setAttribute('color', color);
};
window.addEventListener('load', setInterval(addElements, 4321));
It seems that the line
var color = colors = colors[Math.floor(Math.random() * colors.length)];
contains an error. You should remove = colors
so that the line is
var color = colors[Math.floor(Math.random() * colors.length)];
instead of name.setAttribute('color', color);
use
name.style.color = color;