Design comparison
Solution retrospective
No particular issues, i learned the basic of the fetch method with and API. Still a lot to learn, the "then" method is still a bit obscur for me.
Community feedback
- @GregLyonsPosted over 2 years ago
Nice job! You're using
fetch
just fine here. I found asynchronous JS pretty tricky for awhile. You might find Sid's answer to this StackExchange question informative, as it's a pretty comprehensive answer. His breakdown of the Promise API (steps 1-6) is especially helpful.You might also find
async
/await
syntax more intuitive, which I use in my solution. It lets you, for example, assign the results of the asynchronous action to a variable in a "synchronous" looking manner. From my code:**async** function getAdvice() { const { id, advice, } = **await** fetch('https://api.adviceslip.com/advice') .then(res => res.json()) .then(data => data.slip); ... ... (DOM stuff with my variables) ...
(Note that I'm using object unpacking to get the
id
andadvice
properties fromdata.slip
.) Essentially, I'm able to store the returned value of the lastthen
statement into variables. Usingawait
means that my code won't go forward until the asynchronous action is complete. Without theawait
, what's returned would just be aPromise
, not an object withid
andadvice
attributes, and I couldn't use those variables.To be clear, your solution is also completely correct/understandable. I just wanted to point out this new syntax since a lot of people find it easier to wrap their head around than the old methods of asynchronous programming. You might prefer
Promise
/then
instead, and even if not, you'd still need to understand them a bit to useasync
/await
.One last thing I'll talk about is that your has a couple pesky scrollbars, even though they aren't necessary. They're not a big deal in this case, but they could be annoying in your future projects. Here's how you can get rid of them:
The vertical scrollbar
You set your
<main>
to haveheight: 100vh;
, presumably to center your card vertically. Then, when you add your<footer>
, this is additional content, so that the total space is greater than100vh
, hence the vertical scrolling. Centering things vertically is pretty tricky, and I can't think of a simple solution here. Where I would start would be puttingheight: 100vh
on the<body>
instead of the<main>
(actuallymin-height: 100vh;
; usingheight: 100vh
would be bad practice since it could mess up when content actually does overflow the screen vertically) and then do some sort of positioning to get your footer at the bottom. (Maybe you couldalign-self
it with Flex?) This article gives some helpful information on centering things vertically, though it doesn't cover your exact use case.Anyway, that's where the vertical scroll bar is coming from.
The horizontal scrollbar
I believe this is because you're setting
width: 100vw;
on yourmain
. I believe you're doing this to center the content horizontally, but I don't believe it should be necessary, as block elements (such as<div>
and<p>
) take up all the available horizontal space. I believe<main>
is such an element, so it should already be taking up the width of the screen. I believe what's causing the horizontal scrollbar is that your vertical scrollbar from the previous point is adding on a bit of width to the page, so the width of your page is actually100vw
plus the width of the vertical scrollbar. That causes horizontal overflow.I think removing
width: 100vw;
should fix this. In the future, when you do want scrolling but don't want it to mess with the width, you could use something likeoverflow-y: overlay;
. This will make it cover any content below it though, so keep that in mind.Hope this helps!
Marked as helpful2@geoffreyhachPosted over 2 years ago@GregLyons thanks for the long and constructive review ! I’ll read this again in detail tomorrow to be sure to remember all of those things. Cheers !
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