Skip to content

Commit 18dcc8c

Browse files
week1 exercises finished
1 parent 695ce10 commit 18dcc8c

4 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Dog Photo Gallery</title>
7+
</head>
8+
<body>
9+
<button id="xml">XMLHttpRequest</button>
10+
<button id="axios">Axios</button>
11+
<ul></ul>
12+
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
13+
<script src="dogPhotoGallery.js"></script>
14+
</body>
15+
</html>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
3+
const xmlButton = document.querySelector('#xml');
4+
const axiosButton = document.querySelector('#axios');
5+
const photoList = document.querySelector('ul');
6+
const url = "https://dog.ceo/api/breeds/image/random";
7+
8+
const dogPhotos = (dog) => {
9+
photoList.innerHTML += `<li style="width:10%;">
10+
<img src="${dog.message}">
11+
</li>
12+
`
13+
}
14+
15+
xmlButton.addEventListener('click', () => {
16+
const xhr = new XMLHttpRequest();
17+
xhr.responseType = 'json';
18+
xhr.open('GET', url);
19+
xhr.onload = () => {
20+
if (xhr.status === 200) {
21+
dogPhotos(xhr.response);
22+
}
23+
else {
24+
console.log('Something went wrong! ', xhr.status)
25+
}
26+
}
27+
xhr.send();
28+
});
29+
30+
axiosButton.addEventListener('click', () => {
31+
axios.get(url)
32+
.then(response => dogPhotos(response.data))
33+
.catch(error => console.log(error.message))
34+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
// to launch this js file you just need to define into your html file.
4+
5+
const scriptAxios = document.createElement('script');
6+
scriptAxios.src = "https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js";
7+
document.body.prepend(scriptAxios);
8+
const newFriend = document.createElement('div');
9+
newFriend.style.width = '33%';
10+
document.body.prepend(newFriend);
11+
const axiosButton = document.createElement('button');
12+
axiosButton.innerText = 'Get AxiosRequest';
13+
document.body.prepend(axiosButton);
14+
const xmlButton = document.createElement('button');
15+
xmlButton.innerText = 'Get XMLHttpRequest';
16+
document.body.prepend(xmlButton);
17+
const url = "https://www.randomuser.me/api";
18+
19+
20+
const friendFinder = (friend) => {
21+
newFriend.innerHTML = `<ul>
22+
<li>Gender: ${friend.results[0].gender}</li>
23+
<li>Full Name: ${friend.results[0].name.first} ${friend.results[0].name.last}</li>
24+
<li>Age: ${friend.results[0].dob.age}</li>
25+
<li>Country: ${friend.results[0].location.country}</li>
26+
</ul>
27+
<img src="${friend.results[0].picture.large}">`
28+
}
29+
30+
xmlButton.addEventListener('click', () => {
31+
const xhr = new XMLHttpRequest();
32+
xhr.responseType = 'json'
33+
xhr.open('GET', url);
34+
xhr.onload = () => {
35+
if (xhr.status === 200) {
36+
friendFinder(xhr.response);
37+
}
38+
else {
39+
console.log('Something went wrong! ', xhr.status)
40+
}
41+
}
42+
xhr.send();
43+
})
44+
45+
axiosButton.addEventListener('click', () => {
46+
axios.get(url)
47+
.then(response => friendFinder(response.data))
48+
.catch(error => console.log(error.message))
49+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
// to launch this js file you just need to define into your html file.
4+
5+
const scriptAxios = document.createElement('script');
6+
scriptAxios.src = "https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js";
7+
document.body.prepend(scriptAxios);
8+
const imageTwo = document.createElement('img');
9+
imageTwo.style.width = '30%';
10+
document.body.prepend(imageTwo);
11+
const image = document.createElement('img');
12+
image.style.width = '30%';
13+
document.body.prepend(image);
14+
const url = "https://xkcd.now.sh/?comic=latest";
15+
16+
const xmlRequest = () => {
17+
const xhr = new XMLHttpRequest();
18+
xhr.responseType = 'json';
19+
xhr.open('GET', url);
20+
xhr.onload = () => {
21+
if (xhr.status === 200) {
22+
image.src = xhr.response.img;
23+
}
24+
else {
25+
console.log('Something went wrong! ', xhr.status)
26+
}
27+
}
28+
xhr.send();
29+
}
30+
31+
xmlRequest();
32+
33+
const axiosRequest = () => {
34+
axios.get(url)
35+
.then(response => imageTwo.src = response.data.img)
36+
.catch(error => console.log(error.message))
37+
}
38+
39+
axiosRequest();

0 commit comments

Comments
 (0)