forked from kunjgit/GameZone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color matcher
74 lines (66 loc) · 1.9 KB
/
color matcher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Color Matcher</title>
<link rel="stylesheet" href="styles.css">
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0;
}
.container {
text-align: center;
margin-top: 100px;
}
#colorDisplay {
width: 100px;
height: 100px;
margin: 20px auto;
border: 2px solid black;
}
#result {
margin-top: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<h1>Color Matcher</h1>
<div id="colorDisplay"></div>
<input type="color" id="colorInput">
<button id="matchButton">Match Color</button>
<p id="result"></p>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
const colorDisplay = document.getElementById("colorDisplay");
const colorInput = document.getElementById("colorInput");
const matchButton = document.getElementById("matchButton");
const result = document.getElementById("result");
let targetColor = generateRandomColor();
colorDisplay.style.backgroundColor = targetColor;
matchButton.addEventListener("click", function() {
const userColor = colorInput.value;
if (userColor.toLowerCase() === targetColor.toLowerCase()) {
result.textContent = "Congratulations! You matched the color!";
result.style.color = "green";
} else {
result.textContent = "Sorry! Wrong color. Try again!";
result.style.color = "red";
}
});
function generateRandomColor() {
const letters = "0123456789ABCDEF";
let color = "#";
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
});
</script>
</body>
</html>