Design comparison
Solution retrospective
Any feedback is welcome!
Community feedback
- @shakhboz-shukhratPosted over 1 year ago
Hello there👋! Congratulations on completing this challenge!
The code looks good with some best practices taken into account, including descriptive variable names, use of
const
for immutable variables, andasync/await
in handling an AJAX request.However, there are some errors in the code that need fixing:
axios
is not defined. You need to import it first, like this:import axios from 'axios';
getQuote
andsetIdAndQuote
functions are executed with every click on the button. This is due to wrong placement of the event listener.setIdAndQuote
should not be inside the event listener. To fix this, simply movesetIdAndQuote
outside of the click event listener.It's better to use
let
orconst
for event listeners. That is,$btnDice
should be changed to constbtnDice
.If the import and placement of functions are fixed, the button should work even without the "DOMContentLoaded" event listener.
Here's the corrected code:
const adviceId = document.querySelector(".advice__id"); const adviceQuote = document.querySelector(".advice__quote"); const btnDice = document.querySelector(".btn__dice"); const getQuote = async () => { try { return await axios.get("https://api.adviceslip.com/advice"); } catch (err) { console.error(err); } }; const setIdAndQuote = async () => { const data = await getQuote(); adviceId.textContent = "ADVICE #" + data.data.slip.id; adviceQuote.textContent = `"${data.data.slip.advice}"`; }; setIdAndQuote(); btnDice.addEventListener("click", setIdAndQuote);
So anyway, your solution looks great. Good luck!
Marked as helpful1@ZafkielEdtPosted over 1 year ago@Trueboss Thanks for your feedback!, I will fix the errors.
0
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