Skip to content

Instantly share code, notes, and snippets.

@rodrigoramirez93
Last active May 9, 2020 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigoramirez93/737574cf7fff47ea10a46f4d88667a77 to your computer and use it in GitHub Desktop.
Save rodrigoramirez93/737574cf7fff47ea10a46f4d88667a77 to your computer and use it in GitHub Desktop.
Change color randomly in javascript
//live example at: https://playcode.io/448394
var makeColors = () => {
var minNum = 0;
var maxNum = 9;
var minLet = 65;
var maxLet = 71;
function randomNumber() {
return (Math.floor(Math.random() * (+maxNum - +minNum)) + +minNum).toString();
};
function randomLetter() {
return String.fromCharCode((Math.floor(Math.random() * (+maxLet - +minLet)) + +minLet));
};
var color = [];
color[0] = '#';
for (let i = 1; i < 8; i++) {
color[i] = randomNumber();
color[i + 1] = randomLetter();
}
return color.join('');
}
var miliseconds = 200;
window.setInterval(function () {
document.body.style = 'background-color: ' + makeColors() + '';
}, miliseconds);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment