REST Countries API with color theme switcher
Design comparison
Solution retrospective
Hi guys, this is my first React Challenge, nice experience. I couldn't make the border countries' links work well, in some countries works but in others not. If someone has a tip, I would be very grateful :3
The problem is that the API returns me the border country with just 3 letters, and when I send that to the link, sometimes, find nothing, I don't know how to get the full name of the border country :v
Community feedback
- @jelenkoo10Posted over 2 years ago
I've had a similar problem when I tried to display the border countries, so I implemented a function which receives a country code, goes through all countries data and returns a name of the country whose country code is equal to the one forwarded. Here it is: function searchCountry(code) { let name for (let i = 0; i < countries.length; i++) { if (countries[i].cca3 == code) { name = countries[i].name.common } } return name } You can use this function to display the full name of the country in your Link, rather than only the first three letters. Hope this helps, happy coding! :)
Marked as helpful1@warrenleePosted over 2 years ago@jelenkoo10 Or you can use js array method
filter()
which filters the array of items according to the condition andfirst()
which returns the first item in the array.function searchCountry(code) { return countries.filter(function (country) { return country.cca3 === code; }).first(); }
Marked as helpful2@ValenssPosted over 2 years ago@warrenlee @jelenkoo10 Hi guys! That was very helpful, I'm trying to implement it, huge thanks, did you know also how to display correct the screenshot? Because i didn't see properly the solution screenshot haha.
1@jelenkoo10Posted over 2 years ago@Valenss Yeah I also want to know, my solution seemed to display along with the text :D @warrenlee thanks for another solution, I kinda need to stick with the habit of using filter(), but in this case where only one value is returned, I chose this way. Thanks for the first() method though.
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord