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 React + TS

JJacobPR 240

@JJacobPR

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


Hey!

This app is the beginning of my TS journey and I hope you like it.

As this API tends to have bugs with random data, I just added ID of hint as parameter and generated it myself. 😀

Community feedback

P

@cancomertpay

Posted

Hey Jacob, great work! But, when you send a random number between 1 and 224, if it happens to be 67, you encounter an error because the API isn't perfect. You could try using the 'do while' method like this :

do {
        let randomNumber = Math.floor(Math.random() * 224) + 1;

        const response = await fetch(
          `https://api.adviceslip.com/advice/${randomNumber}`
        );

        if (response.ok) {
          const result = await response.json();
          if (result?.message?.type !== "error") {
            setAdvice(result);
            break;
          }
        } else {
          throw new Error("Something went wrong, please refresh the page");
          break;
        }
      } while (true);

Marked as helpful

0

JJacobPR 240

@JJacobPR

Posted

Hey thanks for notifying me about this!

I will fix this. I guess that's what happens when you try to fix API call without seeing its source code 😂

0

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