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

Done #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Done #18

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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5502
}
65 changes: 64 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
console.log('%c HI', 'color: firebrick')

const imgUrl = "https://dog.ceo/api/breeds/image/random/4"
const breedUrl = "https://dog.ceo/api/breeds/list/all"

let breeds = []

function fetchImages() {
fetch(imgUrl)
.then(resp => resp.json())
.then(json => renderImages(json.message))
}

function renderImages(images) {
images.forEach(imgUrl => {
const dogImage = document.querySelector("#dog-image-container")
const img = document.createElement('img')
img.src = imgUrl
dogImage.appendChild(img)
})
}

function fetchDogBreeds() {
fetch(breedUrl)
.then(resp => resp.json())
.then(json => {
breeds = Object.keys(json.message)
renderBreeds(breeds)

})
.catch(err => console.log('err:', err))
}

function renderBreeds(breeds) {
const dogList = document.getElementById('dog-breeds')
breeds.forEach(breed => {
let li = document.createElement('li')
dogList.appendChild(li)
li.innerText = breed
changeColor(li)
})
}

function changeColor(breed) {
breed.addEventListener("click", function(e) {
breed.style.color = "red"
})
}

function filterFunction() {
let dogList = document.getElementById("dog-breeds")
let breedDropDown = document.getElementById("breed-dropdown")
breedDropDown.addEventListener('change', function(e) {
let letter = e.target.value
let filter = breeds.filter(breed => breed.startsWith(letter))
dogList.innerHTML = ""
renderBreeds(filter)
})
}

document.addEventListener('DOMContentLoaded', function() {
fetchImages();
fetchDogBreeds();
filterFunction();
})