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

Prabu 150

@reallyUndefined

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 everyone,

I had fun with this challenge.

Please do give feedbacks, suggestions or advices to make it better.

Thanks. ✌️

Community feedback

@fazzaamiarso

Posted

Hi Prabu! Great work overall!

You can make your code more robust implementing errror handling on fetch. Here is my implementation.

async function getData() {
  const res = await fetch('https://api.adviceslip.com/advice');
  if(!res.ok) throw Error("There is something wrong!"); //throw error if fetch failed
  return await res.json();
}

const adviceCard = document.querySelector("main");

const udpdateDOM = (slip) =>{ 
  adviceCard.querySelector("span").innerText = `ADVICE #${slip.id}`;
  adviceCard.querySelector("h1").innerText = `"${slip.advice}"`;
}

async function setAdvice() {
//catch the error down here
  try {
    const { slip } = await getData();
    udpdateDOM(slip);
  }catch (err) {
    console.error(err);
  } 
}

Marked as helpful

1
Elaine 11,400

@elaineleung

Posted

Hi Prabu, glad you had fun with this challenge (I did as well), and I think you did well in putting it togoether 🙂

One small issue right now is, I'm viewing it on Firefox and I'm unable to load the next advice, which is a commonly seen issue due to caching. If you have Firefox you can also check it out yourself. What you can do is add a header object in fetch with a line for cache, like this:

const res = await fetch("https://api.adviceslip.com/advice",  { cache: 'no-cache' });

You can also try to add some error handling to your getData() function!

Marked as helpful

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