Logic Problems
Open Weather API
Section titled “Open Weather API”Consider the version of their API that allows you to request information based on a named location instead of Lat/Long details. Their documentation shows three versions of an API call where you can use certain combinations of city name, state code and country code.
https://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}https://api.openweathermap.org/data/2.5/weather?q={city name},{country code}&appid={API key}https://api.openweathermap.org/data/2.5/weather?q={city name},{state code},{country code}&appid={API key}Given the pattern above, create a generalized function that could assemble the query string portion of the URL from a single object input. You may presume that the input data has been verified for correctness beforehand (e.g.: not missing the city name or app id).
View Solution
If you know something about object deconstruction syntax and the built-in array functions in JavaScript, then you might find it can be solved quite easily without needing any if/else statements. Here’s one possible solution.
const buildQueryString = ({city, state, country, appId}) => { const queryParts = [city, state, country]; const queryValue = queryParts.filter(item => !!item).join(','); return `?q=${queryValue}&appid=${appId}`;}