-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
120 lines (98 loc) · 2.91 KB
/
index.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const reportAcudits: Joke[] = [];
interface Joke {
joke: string;
score: number;
date: string;
}
let currentJoke: Joke;
const jokeFixed: HTMLElement = document.getElementById("jokeFixed")!;
const button: HTMLButtonElement = document.querySelector("button")!;
const vote1: HTMLButtonElement = document.getElementById('1') as HTMLButtonElement;
const vote2: HTMLButtonElement = document.getElementById('2') as HTMLButtonElement;
const vote3: HTMLButtonElement = document.getElementById('3') as HTMLButtonElement;
function mostrarDatosApi() {//primera api de chistes
fetch("https://icanhazdadjoke.com/", {
headers: {
"Accept": "application/json"
}
})
.then(res => res.json())
.then(data => {
console.log(data);
currentJoke = {
joke: data.joke,
score: 0,
date: new Date().toISOString(),
};
if (jokeFixed) {
jokeFixed.innerHTML = `" ${currentJoke.joke} "`;
}
});
}
function mostrarDatosApiChuck() {//segunda api de chistes
fetch("https://api.chucknorris.io/jokes/random", {
headers: {
"Accept": "application/json"
}
})
.then(res => res.json())
.then(data => {
console.log(data);
currentJoke = {
joke: data.value,
score: 0,
date: new Date().toISOString(),
};
if (jokeFixed) {
jokeFixed.innerHTML = `" ${currentJoke.joke} "`;
}
});
}
vote1.addEventListener("click", (e: MouseEvent) => {
e.preventDefault();
currentJoke.score = 1;
reportAcudits.push(currentJoke);
console.log(reportAcudits);
});
vote2.addEventListener("click", (e: MouseEvent) => {
e.preventDefault();
currentJoke.score = 2;
reportAcudits.push(currentJoke);
console.log(reportAcudits);
});
vote3.addEventListener("click", (e: MouseEvent) => {
e.preventDefault();
currentJoke.score = 3;
reportAcudits.push(currentJoke);
console.log(reportAcudits);
});
if (button) {
button.addEventListener('click', (e: MouseEvent) => {
e.preventDefault();
const random:number = Math.floor(Math.random() * 10); //Anadi Math.random para alternar los APIS
if(random<=5){
mostrarDatosApi();
}else{
mostrarDatosApiChuck();
}
});
}
mostrarDatosApi();
const weather: HTMLElement = document.getElementById('tiempo')!;
function mostrarDatosTiempo() {
fetch("https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m", {
headers: {
"Accept": "application/json"
}
})
.then(res => res.json())
.then(data => {
const currentTemperature = data.current.temperature_2m;
weather.innerHTML = `Temperatura actual: ${currentTemperature}°C`;
console.log(data.current);
})
.catch(error => {
console.error('Error al obtener datos meteorológicos:', error);
});
}
mostrarDatosTiempo();