Advice Generator - Api Call + Button to generate new.
Design comparison
Solution retrospective
I don't know how to do the lines around the " effectively, I'd like to know how?
The dice didn't center properly, I used some weird % to somehow center it, how can I get the same results better?
The fetch() Javascript bit, it works, yes. But is there a way to simplify this piece of code?
Community feedback
- @itushPosted over 1 year ago
Congratulations on completing the challenge🎉
@LukaKobaidze has already addressed the layout issue.
The Fetch API is a commonly used way to make HTTP requests in web development. There are other libraries and frameworks that can simplify the process of making API requests and handling the response, such as Axios, jQuery, and others. These libraries often provide additional features like request cancellation, automatic JSON parsing, and more. However, I would strongly suggest to clearly understand how Fetch API works first, and then move on to libs/ frameworks as per the requirements.
Line-by-line explanation of the getAdvice():
fetch("https://api.adviceslip.com/advice")
In this line we are using the Fetch API to make a GET request to the Adviceslip.com API at the specified URL ("https://api.adviceslip.com/advice"). This API returns a JSON object with a slip property that contains an id and an advice string.
.then((data) => {
return data.json();
})
In this line we are using the then method to handle the response data returned by the API request. The arrow function passed as an argument to the then method takes a single argument (data) and calls the json method on it to parse the response body as JSON data.
.then((completedata) => {
In this line, we are using another then method to handle the parsed JSON data returned by the previous then method. The arrow function passed as an argument to this then method takes a single argument (completedata) and updates the HTML of the page with the advice ID and text.
console.log(completedata);
Logging completedata object for debugging.
document.getElementById('adid').innerHTML = completedata.slip.id;
Targetting the HTML element with 'adid' ID and sets its innerHTML property to the advice ID from the API response.
document.getElementById('advice').innerHTML = completedata.slip.advice;
Targetting the HTML element with 'advice' ID and sets its innerHTML property to the advice text from the API response.
While it is alright to use internal CSS and JS for small projects, using external CSS and JS file will help you to organize and maintain the code more efficiently. You may want to checkout my article on 12 Important CSS topics for some simple explanations.
Hope to see cool projects from you in the future. Once again you've done a wonderful job🚀 Keep at it... Happy hacking ! 💻
0 - @LukaKobaidzePosted over 1 year ago
To center dice properly, you can use
left
andtransform
properties together:.dice { left: 50%; transform: translateX(-50%); }
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