-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (52 loc) · 1.54 KB
/
index.js
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
// Solution from the last assignment, use as your starter code
// Base URL for CatFacts API
const baseUrl = "https://catfact.ninja";
console.log("Base URL set up for CatFacts API:", baseUrl);
// Fetch a single random cat fact
const getRandomFact = () => {
fetch(`${baseUrl}/fact`)
.then((response) => response.json())
.then((data) => {
console.log("Random Cat Fact:");
console.log(data.fact);
})
.catch((error) => {
console.error("Error fetching random cat fact:", error);
});
};
// Call the function to test it
getRandomFact();
// Fetch multiple random cat facts
const getMultipleFacts = () => {
// Specify the number of facts to fetch (e.g., 3)
const numberOfFacts = 3;
fetch(`${baseUrl}/facts?limit=${numberOfFacts}`)
.then((response) => response.json())
.then((data) => {
console.log(`\n${numberOfFacts} Random Cat Facts:`);
data.data.forEach((fact, index) => {
console.log(`${index + 1}. ${fact.fact}`);
});
})
.catch((error) => {
console.error("Error fetching multiple cat facts:", error);
});
};
// Function call
getMultipleFacts();
// Fetch cat breeds
const getCatBreeds = () => {
fetch(`${baseUrl}/breeds`)
.then((response) => response.json())
.then((data) => {
console.log("\nList of Cat Breeds:");
data.data.forEach((breed, index) => {
console.log(`${index + 1}. ${breed.breed}`);
});
})
.catch((error) => {
console.error("Error fetching cat breeds:", error);
});
};
// Function call
getCatBreeds();