fix(curriculum): allow encoded URLs in Weather App (#60514)

This commit is contained in:
Anideep Kalia 2025-05-27 20:03:52 +05:30 committed by GitHub
parent 90230f491d
commit 5748ca5750
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -92,31 +92,212 @@ NOTE: The tests will take time to complete. As long as you see `// running tests
```js
window.fetch = (url) => {
const city = url.split('/').pop();
const city = decodeURIComponent(url.split("/").pop());
const cityMap = {
'new york': {"coord":{"lon":-74.0059,"lat":40.7127},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"https://cdn.freecodecamp.org/weather-icons/04d.png"}],"base":"stations","main":{"temp":14.33,"feels_like":13.2,"temp_min":13.21,"temp_max":15.54,"pressure":1008,"humidity":53,"sea_level":1008,"grnd_level":1007},"visibility":10000,"wind":{"speed":3.6,"deg":40},"clouds":{"all":100},"dt":1732123426,"sys":{"type":1,"id":4610,"country":"US","sunrise":1732103363,"sunset":1732138471},"timezone":-18000,"id":5128581,"name":"New York","cod":200},
"los angeles": {"coord":{"lon":-118.2437,"lat":34.0522},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"https://cdn.freecodecamp.org/weather-icons/01d.png"}],"base":"stations","main":{"temp":17.24,"feels_like":15.88,"temp_min":14.84,"temp_max":19.62,"pressure":1023,"humidity":33,"sea_level":1023,"grnd_level":1003},"visibility":10000,"wind":{"speed":2.57,"deg":70},"clouds":{"all":0},"dt":1732123664,"sys":{"type":2,"id":2075946,"country":"US","sunrise":1732113063,"sunset":1732150008},"timezone":-28800,"id":5368361,"name":"Los Angeles","cod":200},
chicago: {"coord":{"lon":-87.6298,"lat":41.8781},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"https://cdn.freecodecamp.org/weather-icons/03d.png"}],"base":"stations","main":{"temp":8.91,"feels_like":4.91,"temp_min":7.86,"temp_max":9.44,"pressure":1009,"humidity":50,"sea_level":1009,"grnd_level":987},"visibility":10000,"wind":{"speed":9.39,"deg":285,"gust":12.52},"clouds":{"all":40},"dt":1732123645,"sys":{"type":2,"id":2010190,"country":"US","sunrise":1732106817,"sunset":1732141557},"timezone":-21600,"id":4887398,"name":"Chicago","cod":200},
london: {"coord":{"lon":-0.1278,"lat":51.5074},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"https://cdn.freecodecamp.org/weather-icons/01n.png"}],"base":"stations","main":{"temp":2.62,"feels_like":0.84,"temp_min":1.72,"temp_max":3.49,"pressure":1010,"humidity":81,"sea_level":1010,"grnd_level":1005},"visibility":10000,"wind":{"speed":1.79,"deg":285,"gust":3.13},"clouds":{"all":1},"dt":1732123462,"sys":{"type":2,"id":2075535,"country":"GB","sunrise":1732087658,"sunset":1732118707},"timezone":0,"id":2643743,"name":"London","cod":200},
tokyo: {"coord":{"lon":139.6917,"lat":35.6895},"weather":[{"id":501,"main":"Rain","description":"moderate rain","icon":"https://cdn.freecodecamp.org/weather-icons/10n.png"}],"base":"stations","main":{"temp":8.71,"feels_like":5.38,"temp_min":8.08,"temp_max":9.81,"pressure":1015,"humidity":92,"sea_level":1015,"grnd_level":1014},"visibility":7000,"wind":{"speed":6.69,"deg":330},"rain":{"1h":2.05},"clouds":{"all":75},"dt":1732123711,"sys":{"type":2,"id":268395,"country":"JP","sunrise":1732137787,"sunset":1732174284},"timezone":32400,"id":1850144,"name":"Tokyo","cod":200},
"new york": {
coord: { lon: -74.0059, lat: 40.7127 },
weather: [
{
id: 804,
main: "Clouds",
description: "overcast clouds",
icon: "https://cdn.freecodecamp.org/weather-icons/04d.png",
},
],
base: "stations",
main: {
temp: 14.33,
feels_like: 13.2,
temp_min: 13.21,
temp_max: 15.54,
pressure: 1008,
humidity: 53,
sea_level: 1008,
grnd_level: 1007,
},
visibility: 10000,
wind: { speed: 3.6, deg: 40 },
clouds: { all: 100 },
dt: 1732123426,
sys: {
type: 1,
id: 4610,
country: "US",
sunrise: 1732103363,
sunset: 1732138471,
},
timezone: -18000,
id: 5128581,
name: "New York",
cod: 200,
},
"los angeles": {
coord: { lon: -118.2437, lat: 34.0522 },
weather: [
{
id: 800,
main: "Clear",
description: "clear sky",
icon: "https://cdn.freecodecamp.org/weather-icons/01d.png",
},
],
base: "stations",
main: {
temp: 17.24,
feels_like: 15.88,
temp_min: 14.84,
temp_max: 19.62,
pressure: 1023,
humidity: 33,
sea_level: 1023,
grnd_level: 1003,
},
visibility: 10000,
wind: { speed: 2.57, deg: 70 },
clouds: { all: 0 },
dt: 1732123664,
sys: {
type: 2,
id: 2075946,
country: "US",
sunrise: 1732113063,
sunset: 1732150008,
},
timezone: -28800,
id: 5368361,
name: "Los Angeles",
cod: 200,
},
chicago: {
coord: { lon: -87.6298, lat: 41.8781 },
weather: [
{
id: 802,
main: "Clouds",
description: "scattered clouds",
icon: "https://cdn.freecodecamp.org/weather-icons/03d.png",
},
],
base: "stations",
main: {
temp: 8.91,
feels_like: 4.91,
temp_min: 7.86,
temp_max: 9.44,
pressure: 1009,
humidity: 50,
sea_level: 1009,
grnd_level: 987,
},
visibility: 10000,
wind: { speed: 9.39, deg: 285, gust: 12.52 },
clouds: { all: 40 },
dt: 1732123645,
sys: {
type: 2,
id: 2010190,
country: "US",
sunrise: 1732106817,
sunset: 1732141557,
},
timezone: -21600,
id: 4887398,
name: "Chicago",
cod: 200,
},
london: {
coord: { lon: -0.1278, lat: 51.5074 },
weather: [
{
id: 800,
main: "Clear",
description: "clear sky",
icon: "https://cdn.freecodecamp.org/weather-icons/01n.png",
},
],
base: "stations",
main: {
temp: 2.62,
feels_like: 0.84,
temp_min: 1.72,
temp_max: 3.49,
pressure: 1010,
humidity: 81,
sea_level: 1010,
grnd_level: 1005,
},
visibility: 10000,
wind: { speed: 1.79, deg: 285, gust: 3.13 },
clouds: { all: 1 },
dt: 1732123462,
sys: {
type: 2,
id: 2075535,
country: "GB",
sunrise: 1732087658,
sunset: 1732118707,
},
timezone: 0,
id: 2643743,
name: "London",
cod: 200,
},
tokyo: {
coord: { lon: 139.6917, lat: 35.6895 },
weather: [
{
id: 501,
main: "Rain",
description: "moderate rain",
icon: "https://cdn.freecodecamp.org/weather-icons/10n.png",
},
],
base: "stations",
main: {
temp: 8.71,
feels_like: 5.38,
temp_min: 8.08,
temp_max: 9.81,
pressure: 1015,
humidity: 92,
sea_level: 1015,
grnd_level: 1014,
},
visibility: 7000,
wind: { speed: 6.69, deg: 330 },
rain: { "1h": 2.05 },
clouds: { all: 75 },
dt: 1732123711,
sys: {
type: 2,
id: 268395,
country: "JP",
sunrise: 1732137787,
sunset: 1732174284,
},
timezone: 32400,
id: 1850144,
name: "Tokyo",
cod: 200,
},
};
return Promise.resolve({
ok: city !== 'paris',
json: () => Promise.resolve( cityMap[city]
)}
);
}
ok: city !== "paris",
json: () => Promise.resolve(cityMap[city]),
});
};
// function that construct an object with the id-value pairs that we expect in the page from an object
const helper = wobj => ({
'weather-icon': wobj.weather[0].icon,
'main-temperature': wobj.main.temp || 'N/A',
'feels-like': wobj.main.feels_like || 'N/A',
humidity: wobj.main.humidity || 'N/A',
wind: wobj.wind.speed || 'N/A',
'wind-gust': wobj.wind.gust || 'N/A',
'weather-main': wobj.weather[0].main || 'N/A',
location: wobj.name || 'N/A'
const helper = (wobj) => ({
"weather-icon": wobj.weather[0].icon,
"main-temperature": wobj.main.temp || "N/A",
"feels-like": wobj.main.feels_like || "N/A",
humidity: wobj.main.humidity || "N/A",
wind: wobj.wind.speed || "N/A",
"wind-gust": wobj.wind.gust || "N/A",
"weather-main": wobj.weather[0].main || "N/A",
location: wobj.name || "N/A",
});
```