-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (73 loc) · 2.94 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
fetch("https://apis.scrimba.com/unsplash/photos/random?orientation=landscape&query=nature")
.then(res => res.json())
.then(data => {
document.body.style.backgroundImage = `url(${data.urls.regular})`
document.getElementById("author").textContent = `By: ${data.user.name}`
})
.catch(err => {
// Use a default background image/author
document.body.style.backgroundImage = `url(https://images.unsplash.com/photo-1560008511-11c63416e52d?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMTEwMjl8MHwxfHJhbmRvbXx8fHx8fHx8fDE2MjI4NDIxMTc&ixlib=rb-1.2.1&q=80&w=1080
)`
document.getElementById("author").textContent = `By: Dodi Achmad`
})
function getActivity() {
fetch("https://apis.scrimba.com/bored/api/activity")
.then(response => {
if(!response.ok) {
throw Error("No activity for now")
}
return response.json()
})
.then(data => {
document.getElementById("bored").textContent = data.activity
document.getElementById("bored").classList.add("activity")
})
.catch(err => {
document.getElementById("bored").textContent = "No activity for now"
document.getElementById("bored").classList.add("activity")
})
}
setTimeout(getActivity, 3000)
setInterval( getActivity, 10000)
fetch("https://api.coingecko.com/api/v3/coins/bitcoin")
.then(res => {
if (!res.ok) {
throw Error("Something went wrong")
}
return res.json()
})
.then(data => {
document.getElementById("crypto-top").innerHTML = `
<img src=${data.image.small} />
<span>${data.name}</span>
`
document.getElementById("crypto").innerHTML += `
<p>🎯: $${data.market_data.current_price.usd}</p>
<p>👆: $${data.market_data.high_24h.usd}</p>
<p>👇: $${data.market_data.low_24h.usd}</p>
`
})
.catch(err => console.error(err))
function getCurrentTime() {
const date = new Date()
document.getElementById("time").textContent = date.toLocaleTimeString("en-us", {timeStyle: "short"})
}
setInterval(getCurrentTime, 1000)
navigator.geolocation.getCurrentPosition(position => {
fetch(`https://apis.scrimba.com/openweathermap/data/2.5/weather?lat=${position.coords.latitude}&lon=${position.coords.longitude}&units=metric`)
.then(res => {
if (!res.ok) {
throw Error("Weather data not available")
}
return res.json()
})
.then(data => {
const iconUrl = `http://openweathermap.org/img/wn/${data.weather[0].icon}@2x.png`
document.getElementById("weather").innerHTML = `
<img src=${iconUrl} />
<p class="weather-temp">${Math.round(data.main.temp)}º</p>
<p class="weather-city">${data.name}</p>
`
})
.catch(err => console.error(err))
});