@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
@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 😂