-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (51 loc) · 1.45 KB
/
Copy pathscript.js
File metadata and controls
63 lines (51 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//test
console.log("hello world :o");
//variables...
const sayings = [
"Magic hand waving. Poof pupper!",
"Admire the cuteness!",
"BARK! BARK!",
"Success... a K-9!",
"Sit. Good boy!",
"And awaaaaayyy we gooooo!"
];
//variables that reference elements on our page
const pic = document.querySelector("#doggo-pic");
const button = document.querySelector("#btn");
const label = document.querySelector("#saying");
const breed = document.querySelector("#breed");
//functions...
const getDogPicURL = async () => {
let url = "https://dog.ceo/api/breeds/image/random";
try {
let result = await fetch(url);
let object = await result.json();
return object;
} catch (error) {
console.log(error);
}
};
const randInt = max => {
return Math.floor(Math.random() * Math.floor(max));
};
const generateSaying = () => {
let rand = randInt(sayings.length);
return sayings[rand];
};
const parseBreed = (url) => {
let list = url.split("/");
return list[4];
}
//////////////////////////////////////////////
window.addEventListener("DOMContentLoaded", event => {
button.addEventListener("click", async e => {
//change doggo pic
let post = await getDogPicURL();
let url = post.message;
pic.src = url;
//change saying
label.innerHTML = generateSaying();
//add breed
breed.innerHTML = "Dog Breed: " + parseBreed(url);
});
});