A little bit of CSS magic, a pinch of JS and dash of API.

Solution retrospective
So this wasn't very difficult. But I'd love to know what you think of the API process I used and any advice regarding the JS/API.
Also... this website works perfectly in Chrome, but in Firefox it returns the same advice even if I close and reopen the browser. It's a bit odd and almost impossible to find any solutions to fix it. If you do know, I'd love to hear about it.
Thanks, Gareth
Please log in to post a comment
Log in with GitHubCommunity feedback
- P@dwhenson
Lovely job here. The app works well 🙌
I would just suggest in your JS to add a check to ensure the API has responded correctly and render an error message if not. This isn't too tricky, and I would just change the first
then
to be something like:.then(response => response.ok ? response.json() : Promise.reject("API failed")```
If you then add a
catch
at the end of your fetch function you can render some fallback text in case things don't work for any reason.It's not a big deal, but thinking about how things can go wrong is a good habit to get into.
Cheers 👋
Dave
Marked as helpful - @emanuelegurini
Hi Gareth, seems that Firefox is caching the fetch result. I found this solution on StackOverflow (https://stackoverflow.com/questions/72567690/ui-is-not-updating-on-click-event-that-triggers-a-fetch-on-firefox-developer) but the problem is that property called count is always incremented by one, and, if you start a new session, advice is always the same in a row.
To avoid the same advice in a row, as you can see from my solution, I create a new Random number, and I use it only if my script detects firefox browser.
My solution: https://github.com/EmanueleGurini/js-advicegenerator/blob/master/js/main.js
ps. why am I using 224 as max number? Advices are 224.
Marked as helpful
Join 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