Random Joke Generator using JavaScript – Coding Torque

Share your love

Hello Guys! In this blog, I’m going to explain to you how to make a Joke Generator using JavaScript. This will be a step-by-step guide including HTML and CSS. Let’s get started 🚀.

Before we start, here are some more JavaScript Games you might like to create:

1. Snake Game using JavaScript

2. 2D Bouncing Ball Game using JavaScript

3. Rock Paper Scissor Game using JavaScript

4. Tic Tac Toe Game using JavaScript

5. Whack a Mole Game using JavaScript

 

I would recommend you don’t just copy and paste the code, just look at the code and type by understanding it.

 

HTML Code 

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome Icons  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
        integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
        crossorigin="anonymous" />

    <!-- Google Fonts  -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">

    <title>Random Joke Generator - @code.scientist x @codingtorque</title>
</head>

<body>
    <div class="container">
        <h1>Random Joke Generator</h1>
        <p> id="joke">
            You can get up to half a million dollars on the black market if you possess an authentic Chuck Norris pube.
        </p>
        <button> type="submit" class="btn" onclick="fetchJoke()">New joke</button>
    </div>

</body>

</html>

Output Till Now

CSS Code 

Create a file style.css and paste the code below.

* {
    font-family: 'Poppins', sans-serif;
}

body {
    background-color: #111827;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
}

.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    border: 2px solid deepskyblue;
    padding: 40px;
    border-radius: 20px;
    width: 30rem;
}

#joke {
    font-style: italic;
}

.btn {
    height: 3rem;
    width: 8rem;
    border: 2px solid deepskyblue;
    background: deepskyblue;
    color: white;
    font-size: 16px;
    border-radius: 7px;
    padding: 8px;
    margin-top: 30px;
    cursor: pointer;
}

JavaScript Code 

Create a file script.js and paste the code below.
const fetchJoke = async () => {
    let response = await fetch("https://api.chucknorris.io/jokes/random");
    let data = await response.json()
    document.getElementById("joke").innerText = data.value;
}
Written by: Piyush Patil
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpful💖
Share your love