Skip to content

Commit

Permalink
Javascript completed
Browse files Browse the repository at this point in the history
  • Loading branch information
AtharvaShinde253 committed Dec 24, 2023
1 parent 59ee268 commit a314e30
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ <h1 class="heading">Jokes Generator</h1>
<p class="joke" id="joke">Joke</p>
<button class="btn" id="btn">Tell me a joke</button>
</div>
<script src="script.js"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions script.js
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);

0 comments on commit a314e30

Please sign in to comment.