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 app

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


This project does not present too many difficulties, but it is helpful to practice requests. One of the things that I would have liked to be able to do is that every time the button is clicked, new advice is generated instead of always showing the same one. If anyone knows how to do that I would love to learn how to do it. Greetings

Community feedback

@Ayman-Shakil192

Posted

Hey Galache👋!

Congratulations on successfully completing this challenge. Your solution looks amazing!

Now to answer you query , To generate new advice every time the button is clicked, you can modify the JavaScript code as follows:

const id = document.querySelector('h1');
const advice = document.querySelector('p');
const dice = document.querySelector('.dice');

function generateAdvice() {
  fetch(`https://api.adviceslip.com/advice`)
    .then(response => response.json())
    .then(data => {
        console.log(data.slip.id);
        console.log(data.slip.advice);
        id.textContent = `Advice #${data.slip.id}`;
        advice.textContent = `"${data.slip.advice}"`;
    })
    .catch(error => console.error(error));
}

dice.addEventListener('click', generateAdvice);

// generate advice on page load (optional)
generateAdvice();

Here, we define a new function generateAdvice() that fetches advice from the API and updates the HTML with the new advice. We then attach this function to the button's click event listener.

Finally, we call generateAdvice() once when the page loads to generate the initial advice. This step is optional

With this modification, every time the button is clicked, a new advice will be fetched and displayed on the page.

Hope this helps. Do let me know if it works!

0

@fico444

Posted

@Ayman-Shakil192 Hi, thanks for your response. I tried that way (putting the request in a function that is called every time the dice is clicked) before posting the solution to frontend mentor, for some reason it didn't work. Seeing your answer, I exactly copied the code but it doesn't work either. It always shows the same result 🤷‍♀️

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