Skip to content

Commit d3efdb7

Browse files
feat: solved 3rd stage of the Hyperskill project "Dog Glossary"
1 parent 16fbc25 commit d3efdb7

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

hyperskill/07_dog_glossary/03/.gitkeep

Whitespace-only changes.

hyperskill/07_dog_glossary/03/app.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
8+
const dogApiBaseUrl = 'https://dog.ceo/api';
9+
10+
randomDogButton.addEventListener('click', async function() {
11+
const response = await fetch(`${dogApiBaseUrl}/breeds/image/random`);
12+
13+
if (!response.ok) {
14+
contentDiv.innerHTML = 'Error fetching random dog';
15+
return;
16+
}
17+
18+
const data = await response.json();
19+
20+
if (data.status === 'success') {
21+
contentDiv.innerHTML = `<img src="${data.message}" alt="Random dog" />`;
22+
} else {
23+
contentDiv.innerHTML = 'Error fetching random dog';
24+
}
25+
});
26+
27+
showBreedButton.addEventListener('click', async function() {
28+
const breed = inputBreed.value.toLowerCase();
29+
const response = await fetch(`${dogApiBaseUrl}/breed/${breed}/images/random`);
30+
31+
if (!response.ok) {
32+
contentDiv.innerHTML = '<p>Breed not found!</p>';
33+
return;
34+
}
35+
36+
const data = await response.json();
37+
38+
if (data.status === 'success') {
39+
contentDiv.innerHTML = `<img src="${data.message}" alt="${breed}" />`;
40+
} else {
41+
contentDiv.innerHTML = '<p>Breed not found!</p>';
42+
}
43+
});
44+
45+
showSubBreedButton.addEventListener('click', async function() {
46+
const breed = inputBreed.value.toLowerCase();
47+
const response = await fetch(`${dogApiBaseUrl}/breed/${breed}/list`);
48+
49+
if (!response.ok) {
50+
contentDiv.innerHTML = '<p>Breed not found!</p>';
51+
return;
52+
}
53+
54+
const data = await response.json();
55+
56+
if (data.status === 'success' && data.message.length > 0) {
57+
let subBreddsList = '<ol>';
58+
data.message.forEach(subBreed => {
59+
subBreddsList += `<li>${subBreed}</li>`;
60+
});
61+
subBreddsList += '</ol>';
62+
contentDiv.innerHTML = subBreddsList;
63+
} else {
64+
contentDiv.innerHTML = '<p>No sub-breeds found!</p>';
65+
}
66+
});
67+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
<div id="content"></div>
16+
</body>
17+
</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)