Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Practice DOM/Fetch Challenges #13

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,29 @@ <h1>Dog CEO</h1>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
<option value="e">e</option>
<option value="f">f</option>
<option value="g">g</option>
<option value="h">h</option>
<option value="i">i</option>
<option value="j">j</option>
<option value="k">k</option>
<option value="l">l</option>
<option value="m">m</option>
<option value="n">n</option>
<option value="o">o</option>
<option value="p">p</option>
<option value="q">q</option>
<option value="r">r</option>
<option value="s">s</option>
<option value="t">t</option>
<option value="u">u</option>
<option value="v">v</option>
<option value="w">w</option>
<option value="x">x</option>
<option value="y">y</option>
<option value="z">z</option>

</select>

<ul id="dog-breeds">
Expand Down
64 changes: 63 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
console.log('%c HI', 'color: firebrick')
document.addEventListener('DOMContentLoaded', function() {
const imgUrl = "https://dog.ceo/api/breeds/image/random/4";
const breedUrl = "https://dog.ceo/api/breeds/list/all";
let dogList = document.getElementById('dog-breeds')
let dogContainer = document.getElementById('dog-image-container')

function displayDogs() {
fetch(imgUrl)
.then(res => res.json())
.then(dogs => {
let dogPics = Object.entries(dogs.message)

dogPics.forEach(dog => {
let img = document.createElement('img')
img.src = dog[1]
img.style.height = '25em'
img.style.width = '25em'

dogContainer.appendChild(img)
})
})
}

function displayBreedList() {
fetch(breedUrl)
.then(res => res.json())
.then(dogBreed => {
let dogObj = dogBreed.message
for(const dogBreed in dogObj){
let li = document.createElement('li')
li.innerText = dogBreed

dogList.appendChild(li)

li.addEventListener('click', () => {
li.style.color = 'green'
})

function sortBreeds() {
let select = document.getElementById('breed-dropdown')
select.addEventListener('change', () => {
dogList.innerText = ''
let letter = select.value

for(const dogBreed in dogObj) {
if(dogBreed[0] === letter) {
let li = document.createElement('li')
li.innerText = dogBreed
dogList.appendChild(li)
li.addEventListener('click', () => {
li.style.color = 'green'
})
}
}
})
}
} sortBreeds()
Comment on lines +39 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely a cool way of doing this, but to make it more readable you could make the dogObj globally available and extract this function. That would make the code more readable and easier to follow.

})
}
displayBreedList()
displayDogs()
})