Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Advice Generator using Vanilla JS, CSS, HTML

#sass/scss
Huynh Nguyen• 430

@Nghuynh07

Desktop design screenshot for the Advice generator app coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
  • API
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


I learned about clamp() in CSS. Its such a cool property. It basically set min/max of the font-size with just one line of CSS. According to MDN it takes in 3 parameters: min, preferred, and max value. This is great for responsive design because it'll size up/down bas on the size of the viewport.

I made an error while I was doing the HTML markup by putting the dice inside a form. By doing this, it actually prevented the dice from generating the quote which is not what I wanted. I wanted the quote to generate each time the button is clicked. Once I realized it, I remove the form element, switched 'submit' to 'click' event then it worked as intended.

I couldn't get the quotes to work correctly. They were too far apart when I tried adding them inside <span> element. Please advise :)

Community feedback

Fazza Razaq Amiarso• 2,360

@fazzaamiarso

Posted

Hello @Nghuynh07! Clean solution!

I have some tips for you.

  • For accessibility reasons, replace the button div to actual button element.
  • When you are using form, the submit event is supposed to be attached to the form itself. Button doesn't have a submit event.

I hope it helps! Cheers!

0
Doston Nabotov• 950

@dostonnabotov

Posted

Hi, there! That looks great. Yeah, clamp is great. I am just wondering how you got it to work. I mean, I am not quite good at JSON stuff. Can you explain how did fetch or got the advice from other data? Could you share the link if you have learned somewhere?

0

Huynh Nguyen• 430

@Nghuynh07

Posted

Hello @dostonnabotov const res = await fetch(https://api.adviceslip.com/advice/${number});

The line above is what I used to fetch data. The fetch function takes in 2 argument fetch(url, and options). The URL is the API source that I use to fetch the data from. If you visit the link w/o '${number}'; there is more to explore. Options is generally where you indicate which request method are you trying to obtain. For our case, it is a "GET" method. By default if you don't put anything after the URL then its default to a "GET" method. If you want to you can it would look like this:

const options = { method: "GET" }

There are others too such as POST, PUT, DELETE, PATCH etc..

The fetch method returns a promise. A promise is something that will happen in the future. For instance, when you are requesting information/data from an external source, it takes time for the HTTP requests to send back and forth data. To prevent our codes from being blocked while fetching information, promises will run in the background asynchronous so the rest of our codes will continue to run.

const data = await res.json();

The line above is right after the fetch() function in which it will return me an object in json format. From there I use javascript destructuring to extract data I got back the promise:

const {
  slip: { id, advice },
} = data;

'data' is the object, and within that object contains other objects and the one that we need for this project is slip and inside slip are id and advice.

const number = Math.floor(Math.random() * 200 + 1); const res = await fetch(https://api.adviceslip.com/advice/${number});

The line above generate a random number which I use interpolation to programmatically changes the quote each time it is clicked. And to have the quote generate upon the app opens, I call the function right away.

One thing to note though is that fetch() alone doesn't do anything. You need to chain it with .then().catch(). This is because a promises has 3 phases, pending, fulfil or reject. .then() fulfill the promise while .catch() will catch the error. This is where it gets a little tricky with error handling in javascript as well. I didn't use .then or .catch() because I am using async/await. This is another wait to chaining promises in javascript.

Sorry Its a lot to read. I recommend taking a course on udemy from jonas schmedtmann: Node JS course and his javascript course. He explains everything so well. You will learn a lot from him. I hope this help.

1

Please log in to post a comment

Log in with GitHub
Discord logo

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