Skip to content

Commit 2452779

Browse files
authored
Merge pull request #33 from Banisha19/banisha/add-randomColor
Add banisha/randomColor.js — JavaScript-Apps — by Banisha (Hacktoberfest)
2 parents bf22e2d + 7c0817f commit 2452779

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

banisha/randomColor.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Added by: Banisha — Hacktoberfest 2025
2+
// File: JavaScript-Apps/banisha/randomColor.js
3+
// Description: Generates a random color and applies it to the page background.
4+
// How to use: include this script in any HTML page, or paste into browser console.
5+
6+
(function () {
7+
function randomHexColor() {
8+
// generate a random integer between 0 and 0xFFFFFF, convert to hex, pad to 6 chars
9+
const hex = Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, '0');
10+
return `#${hex}`;
11+
}
12+
13+
function applyRandomBackground() {
14+
const color = randomHexColor();
15+
if (typeof document !== 'undefined' && document.body) {
16+
document.body.style.backgroundColor = color;
17+
}
18+
console.log('Applied background color:', color);
19+
return color;
20+
}
21+
22+
// Auto-run when loaded in a browser
23+
if (typeof window !== 'undefined') {
24+
window.addEventListener('load', () => {
25+
applyRandomBackground();
26+
});
27+
}
28+
29+
// Export for Node / manual calls
30+
if (typeof module !== 'undefined' && module.exports) {
31+
module.exports = { randomHexColor, applyRandomBackground };
32+
} else {
33+
// attach to window for easy manual testing in browser console
34+
window.__randomColor = { randomHexColor, applyRandomBackground };
35+
}
36+
})();

0 commit comments

Comments
 (0)