scrimba
Frontend Career Path
Working with APIs
Promise Rejection
Weather - CSS
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

AboutCommentsNotes
Weather - CSS
Expand for more info
index.js
run
preview
console
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`
})

fetch("https://api.coingecko.com/api/v3/coins/dogecoin")
.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=imperial`)
.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>${Math.round(data.main.temp)}º</p>
<p>${data.name}</p>
`
})
.catch(err => console.error(err))
});

/**
* Challenge: Try to lay out the weather similar to how
* Momentum does it.
*/
Console
{coord:
{lon:
-111.9162
, lat:
40.5269
}
, weather:
[
{id:
800
, main:
"Clear"
, description:
"clear sky"
, icon:
"01d"
}
]
, base:
"stations"
, main:
{temp:
83.25
, feels_like:
80.62
, temp_min:
78.62
, temp_max:
88
, pressure:
1010
, humidity:
17
}
, visibility:
10000
, wind:
{speed:
11.01
, deg:
167
, gust:
21.99
}
, clouds:
{all:
1
}
, dt:
1623181238
, sys:
{type:
2
, id:
2003861
, country:
"US"
, sunrise:
1623153431
, sunset:
1623207400
}
, timezone:
-21600
, id:
5780557
, name:
"Riverton"
, cod:
200
}
,
/index.html
-8:07