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 Fetch API HTML CSS JavaScript

Lisa Allen 520

@CrypticMango

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


Just wanting to know if there was anything that I could have done better in my JS.

Community feedback

@HectorMartinDama

Posted

Hello @CrypticMango, your file js is good but I would change the api request into a function. That way you don't need to reload the page every time you click the button.

File js

function getApi(){
    fetch('https://api.adviceslip.com/advice/' + Math.floor(Math.random() * 200))
    .then(res => res.json())
    .then(res =>  {
        document.getElementById("phrase").innerHTML= '"' + res.slip.advice + '"'
        document.getElementById("id").innerHTML= 'ADVICE #'+ res.slip.id
    })
    .catch(err => console.log(err))
}

The random number is because if you do not indicate a specific advice, it does not work correctly.

index.html

<button onclick="getApi()"></button>

Call the function in the button

For the rest it is perfect.

GitHub Repo Page preview

Marked as helpful

0

Lisa Allen 520

@CrypticMango

Posted

@HectorMartinDama Okay thank you for the tip I will add it to my solution.

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