-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59ee268
commit a314e30
Showing
2 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const btnEl = document.getElementById("btn"); | ||
const jokeEl = document.getElementById("joke"); | ||
|
||
const apiKey = "4kqGcJx8uDXo3XIskcbzokAz7rN8nWJs3PL9Mcll"; | ||
|
||
const options = { | ||
method: "GET", | ||
headers: { | ||
"X-Api-Key": apiKey, | ||
}, | ||
}; | ||
|
||
const apiURL = "https://api.api-ninjas.com/v1/dadjokes?limit=1"; | ||
|
||
async function getJoke() { | ||
try { | ||
jokeEl.innerText = "Updating..."; | ||
btnEl.disabled = true; | ||
btnEl.innerText = "Loading..."; | ||
const response = await fetch(apiURL, options); | ||
const data = await response.json(); | ||
|
||
btnEl.disabled = false; | ||
btnEl.innerText = "Tell me a joke"; | ||
|
||
jokeEl.innerText = data[0].joke; | ||
} catch (error) { | ||
jokeEl.innerText = "An error happened, try again later"; | ||
btnEl.disabled = false; | ||
btnEl.innerText = "Tell me a joke"; | ||
console.log(error); | ||
} | ||
} | ||
|
||
btnEl.addEventListener("click", getJoke); |