Skip to content

Commit 9bed5a8

Browse files
feat: solved 4th stage of the Hyperskill project "Dog Glossary"
1 parent d3efdb7 commit 9bed5a8

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

hyperskill/07_dog_glossary/04/.gitkeep

Whitespace-only changes.

hyperskill/07_dog_glossary/04/app.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
window.onload = function () {
2+
const randomDogButton = document.getElementById('button-random-dog');
3+
const contentDiv = document.getElementById('content');
4+
const inputBreed = document.getElementById('input-breed');
5+
const showBreedButton = document.getElementById('button-show-breed');
6+
const showSubBreedButton = document.getElementById('button-show-sub-breed');
7+
const showAllButton = document.getElementById('button-show-all');
8+
9+
const dogApiBaseUrl = 'https://dog.ceo/api';
10+
11+
randomDogButton.addEventListener('click', async function () {
12+
const response = await fetch(`${dogApiBaseUrl}/breeds/image/random`);
13+
14+
if (!response.ok) {
15+
contentDiv.innerHTML = 'Error fetching random dog';
16+
return;
17+
}
18+
19+
const data = await response.json();
20+
21+
if (data.status === 'success') {
22+
contentDiv.innerHTML = `<img src="${data.message}" alt="Random dog" />`;
23+
} else {
24+
contentDiv.innerHTML = 'Error fetching random dog';
25+
}
26+
});
27+
28+
showBreedButton.addEventListener('click', async function () {
29+
const breed = inputBreed.value.toLowerCase();
30+
const response = await fetch(`${dogApiBaseUrl}/breed/${breed}/images/random`);
31+
32+
if (!response.ok) {
33+
contentDiv.innerHTML = '<p>Breed not found!</p>';
34+
return;
35+
}
36+
37+
const data = await response.json();
38+
39+
if (data.status === 'success') {
40+
contentDiv.innerHTML = `<img src="${data.message}" alt="${breed}" />`;
41+
} else {
42+
contentDiv.innerHTML = '<p>Breed not found!</p>';
43+
}
44+
});
45+
46+
showSubBreedButton.addEventListener('click', async function () {
47+
const breed = inputBreed.value.toLowerCase();
48+
const response = await fetch(`${dogApiBaseUrl}/breed/${breed}/list`);
49+
50+
if (!response.ok) {
51+
contentDiv.innerHTML = '<p>Breed not found!</p>';
52+
return;
53+
}
54+
55+
const data = await response.json();
56+
57+
if (data.status === 'success' && data.message.length > 0) {
58+
let subBreddsList = '<ol>';
59+
data.message.forEach(subBreed => {
60+
subBreddsList += `<li>${subBreed}</li>`;
61+
});
62+
subBreddsList += '</ol>';
63+
contentDiv.innerHTML = subBreddsList;
64+
} else {
65+
contentDiv.innerHTML = '<p>No sub-breeds found!</p>';
66+
}
67+
});
68+
69+
showAllButton.addEventListener('click', async function () {
70+
const response = await fetch(`${dogApiBaseUrl}/breeds/list/all`);
71+
const data = await response.json();
72+
const breeds = data.message;
73+
let allBreeds = '<ol>';
74+
for (const breed in breeds) {
75+
if (breeds[breed].length > 0) {
76+
allBreeds += `<li>${breed}<ul>`;
77+
breeds[breed].forEach(subBreed => {
78+
allBreeds += `<li>${subBreed}</li>`;
79+
});
80+
allBreeds += '</ul></li>';
81+
} else {
82+
allBreeds += `<li>${breed}</li>`;
83+
}
84+
}
85+
allBreeds += '</ol>';
86+
contentDiv.innerHTML = allBreeds;
87+
});
88+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Dog Glossary</title>
6+
<link rel="stylesheet" href="style.css">
7+
<script src="app.js" defer></script>
8+
</head>
9+
<body>
10+
<h1>Dog Glossary</h1>
11+
<button id="button-random-dog">Show Random Dog</button>
12+
<input type="text" id="input-breed" placeholder="Enter a breed">
13+
<button id="button-show-breed">Show Breed</button>
14+
<button id="button-show-sub-breed">Show Sub-Breed</button>
15+
<button id="button-show-all">Show All Breeds</button>
16+
<div id="content"></div>
17+
</body>
18+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* {
2+
box-sizing: border-box;
3+
}

0 commit comments

Comments
 (0)