-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatFacts.js
More file actions
54 lines (44 loc) · 1.12 KB
/
catFacts.js
File metadata and controls
54 lines (44 loc) · 1.12 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
// Getting catFacts
async function getFacts(){
try{
const responce =await fetch("https://catfact.ninja/fact");
if(!responce.ok){
throw new Error("Faild to tech that API")
}{
const data =await responce.json();
return data;
}
}
catch (error){
console.error("there is some error ");
throw error;
}
}
// Getting 10 CatFacts
async function fetchFacts(){
const calFcats=[];
for(let i=0;i<10;i++){
try{
const FactData=await getFacts();
calFcats.push(FactData);
}catch (error){
console.error("There is some erroe happen bla bla" ,error);
}
}
return calFcats;
}
function sortFacts(facts){
return facts.slice().sort((a,b)=>a.length - b.length);
}
async function main(){
try{
const catFacts = await fetchFacts();
const sortedFacts=sortFacts(catFacts);
sortedFacts.forEach((fact,index)=>{
console.log(`Fact ${index+1}:length - ${fact.length}, catFact - ${fact.fact}`)
});
}catch(error){
console.log(error);
}
}
main();