Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLuis Giraldo
Full Stack JavaScript Techdegree Graduate 27,531 PointsparseInt will omit 0 at the beginning of a zip code
This is related to openweather API solution on the teacher's notes Hi guys, I just wanted to let you know, that if your query has a zip code for example 01581,
const zipCode = parseInt(query);
if (!isNaN(zipCode)) {
parameters.zip = zipCode + ',us';
} else {
parameters.q = query + ',us';
}
const url = `https://api.openweathermap.org/data/2.5/weather?${querystring.stringify(parameters)}`;
console.log(url);
The parseInt will ignore the zero and will return 1581, being an invalid zip code in United States, (I think it has to be 5 digits), so, the url will have : ?zip=1581%2cus instead of ?zip=01581%2cus The API response will be statusCode 404 So make sure to validate that you have a 5 digits zip code before adding it to the url.
2 Answers
Zimri Leijen
11,835 PointsRather than converting it to an int, it's easier to just check it with regex.
/^\d{5}$/.test(query)
Mark Wood
Full Stack JavaScript Techdegree Student 11,100 PointsGreat solution, Zimri. I came up with an alternative that also seems to work:
if (parseInt(query)) {
parameters.zip = query;
} else {
parameters.q = query + ',us';
}