<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Zebra Puzzle</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
text-align: center;
}
.puzzle-description {
margin-bottom: 20px;
}
table {
width: 100%;
max-width: 600px;
margin: auto;
border-collapse: collapse;
}
th, td {
border: 1px solid #ccc;
text-align: center;
width: 33%;
padding: 8px;
}
select {
width: 100%;
padding: 4px;
}
.check-button {
display: block;
margin: 20px auto;
padding: 10px 20px;
font-size: 16px;
}
.result {
text-align: center;
font-size: 18px;
font-weight: bold;
min-height: 50px;
}
</style>
</head>
<body>
<h1>Zebra Puzzle</h1>
<div class="puzzle-description">
<p>There are three houses in a row, each with a different color: <strong>Red</strong>, <strong>Green</strong>, and <strong>Blue</strong>.</p>
<p>Each house is owned by a person of different nationality: <strong>English</strong>, <strong>Spanish</strong>, and <strong>Japanese</strong>.</p>
<p>Each owner drinks a different beverage: <strong>Tea</strong>, <strong>Coffee</strong>, and <strong>Milk</strong>.</p>
<p>Each owner has a different pet: <strong>Dog</strong>, <strong>Cat</strong>, and <strong>Bird</strong>.</p>
<h3>Clues:</h3>
<ol>
<li>The Englishman lives in the red house.</li>
<li>The Japanese owns the dog.</li>
<li>The Spanish drinks tea.</li>
<li>The green house is to the left of the blue house.</li>
<li>The owner of the green house drinks coffee.</li>
<li>The person who has a cat lives next to the person who owns the bird.</li>
</ol>
<p><strong>Question:</strong> Who owns the bird?</p>
</div>
<table>
<thead>
<tr>
<th>House 1</th>
<th>House 2</th>
<th>House 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select id="color1">
<option value="">Select Color</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
</td>
<td>
<select id="color2">
<option value="">Select Color</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
</td>
<td>
<select id="color3">
<option value="">Select Color</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
</select>
</td>
</tr>
<tr>
<td>
<select id="nationality1">
<option value="">Select Nationality</option>
<option value="English">English</option>
<option value="Spanish">Spanish</option>
<option value="Japanese">Japanese</option>
</select>
</td>
<td>
<select id="nationality2">
<option value="">Select Nationality</option>
<option value="English">English</option>
<option value="Spanish">Spanish</option>
<option value="Japanese">Japanese</option>
</select>
</td>
<td>
<select id="nationality3">
<option value="">Select Nationality</option>
<option value="English">English</option>
<option value="Spanish">Spanish</option>
<option value="Japanese">Japanese</option>
</select>
</td>
</tr>
<tr>
<td>
<select id="beverage1">
<option value="">Select Beverage</option>
<option value="Tea">Tea</option>
<option value="Coffee">Coffee</option>
<option value="Milk">Milk</option>
</select>
</td>
<td>
<select id="beverage2">
<option value="">Select Beverage</option>
<option value="Tea">Tea</option>
<option value="Coffee">Coffee</option>
<option value="Milk">Milk</option>
</select>
</td>
<td>
<select id="beverage3">
<option value="">Select Beverage</option>
<option value="Tea">Tea</option>
<option value="Coffee">Coffee</option>
<option value="Milk">Milk</option>
</select>
</td>
</tr>
<tr>
<td>
<select id="pet1">
<option value="">Select Pet</option>
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Bird">Bird</option>
</select>
</td>
<td>
<select id="pet2">
<option value="">Select Pet</option>
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Bird">Bird</option>
</select>
</td>
<td>
<select id="pet3">
<option value="">Select Pet</option>
<option value="Dog">Dog</option>
<option value="Cat">Cat</option>
<option value="Bird">Bird</option>
</select>
</td>
</tr>
</tbody>
</table>
<button class="check-button" onclick="checkSolution()">Check Solution</button>
<div class="result" id="result"></div>
<script>
// Arrays of options
const colors = ["Red", "Green", "Blue"];
const nationalities = ["English", "Spanish", "Japanese"];
const beverages = ["Tea", "Coffee", "Milk"];
const pets = ["Dog", "Cat", "Bird"];
// Function to check the user's solution
function checkSolution() {
// Get values from the selects
let houses = [];
for (let i = 1; i <=3; i++) {
houses.push({
color: document.getElementById('color' + i).value,
nationality:document.getElementById('nationality' + i).value,
beverage: document.getElementById('beverage' + i).value,
pet: document.getElementById('pet' + i).value
});
}
// Check if all selections are made
for (let house of houses) {
if (!house.color || !house.nationality || !house.beverage || !house.pet) {
document.getElementById('result').innerText = "Please fill in all fields.";
return;
}
}
// Check for duplicates in each category
let colorsUsed = new Set();
let nationalitiesUsed = new Set();
let beveragesUsed = new Set();
let petsUsed = new Set();
for (let house of houses) {
colorsUsed.add(house.color);
nationalitiesUsed.add(house.nationality);
beveragesUsed.add(house.beverage);
petsUsed.add(house.pet);
}
if (colorsUsed.size < 3 || nationalitiesUsed.size < 3 || beveragesUsed.size < 3 || petsUsed.size < 3) {
document.getElementById('result').innerText = "Each attribute must be unique across houses.";
return;
}
// Now check the clues
// Clue 1: The Englishman lives in the red house.
let clue1 = houses.some(h => h.nationality === 'English' && h.color === 'Red');
// Clue 2: The Japanese owns the dog.
let clue2 = houses.some(h => h.nationality === 'Japanese' && h.pet === 'Dog');
// Clue 3: The Spanish drinks tea.
let clue3 = houses.some(h => h.nationality === 'Spanish' && h.beverage === 'Tea');
// Clue 4: The green house is to the left of the blue house.
// Find positions of green and blue houses
let greenIndex = houses.findIndex(h => h.color === 'Green');
let blueIndex = houses.findIndex(h => h.color === 'Blue');
let clue4 = greenIndex !== -1 && blueIndex !== -1 && greenIndex +1 === blueIndex;
// Clue 5: The owner of the green house drinks coffee.
let clue5 = houses.some(h => h.color === 'Green' && h.beverage === 'Coffee');
// Clue 6: The person who has a cat lives next to the person who owns the bird.
let catIndex = houses.findIndex(h => h.pet === 'Cat');
let birdIndex = houses.findIndex(h => h.pet === 'Bird');
let clue6 = (catIndex !== -1 && birdIndex !== -1 && Math.abs(catIndex - birdIndex) ===1);
if (clue1 && clue2 && clue3 && clue4 && clue5 && clue6) {
// All clues are satisfied
// Now answer the question: Who owns the bird?
let ownerOfBird = houses[birdIndex].nationality;
document.getElementById('result').innerText = "Correct! The " + ownerOfBird + " owns the bird.";
} else {
document.getElementById('result').innerText = "Incorrect solution. Please try again.";
}
}
</script>
</body>
</html>
This code sets up a simple interactive Zebra Puzzle where you can select options for each house. After filling in all the fields, click the "Check Solution" button to see if your solution satisfies all the clues. The program will check your solution against the clues and tell you if it's correct or not.