## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today. # The Refactor Challenge, Part 2

Heloo

```// this is how i did it
```
```    var html = '  ';
var rgbColor;
var flag = true;

function repeatedCode(){return Math.floor(Math.random() * 256 );}

var otec = function(){
rgbColor = 'rgb(' + repeatedCode() + ',' + repeatedCode() + ',' + repeatedCode() + ')';
html += '<div style="background-color:' + rgbColor + '"></div>';
}

for (var x =1; x <=10 ;x++){
otec();
if (x == 10){
flag = false;
}
}
document.write(html);
```

I was wondering if i can find a way of making a delay and changing the different colors without reloading the entire page.

Yes you can! you can use the SetInterval function in JavaScript. Here is an example on how to use it:

```var x = document.body;
var color = ["blue", "green", "yellow", "red"];
setInterval(function() {
for(let y = 0; y < 4; y++){
x.style.backgroundColor = color[Math.floor(Math.random() * 3)];
}
}, 500) ;
```

This example shows how to change the color on the body element, you can change this to be your divs. what we do is create an array of colors, then use the setInterval function to loop through the element we want to change. This is a basic example, but you can change it as your like. when you start using array methods please feel free to update this code as your progress.

No problem! If you have any questions please feel free to ask. I finally got it. Jacob Mishkin

```let  html = '';
let Arrays = ['A','B','C','D'];
let Color = ['Lightgrey','Lightgreen','coral','purple'];

let random = () =>   Math.floor(Math.random() * 4 );

let print = message => {
let bodyID = document.getElementById('color');
bodyID.innerHTML = message;
}

for (let x = 0; x < Arrays.length; x++){

html +=  `<div class = \${Arrays[x]}></div>`;
}

// Create the Divs Random Colors
setInterval(() => {

for (let x = 0; x  <  Arrays.length;  x++){

let divColorControl  =  document.querySelector(` div.\${Arrays[x]} `);
divColorControl.style.backgroundColor  =  Color[ random() ]
}
}, 1000)

print(html);
```