Home >Web Front-end >JS Tutorial >Building an Advice Generator Website

Building an Advice Generator Website

PHPz
PHPzOriginal
2024-08-08 06:50:52728browse

Building an Advice Generator Website

Introduction

Hello, fellow developers! Today, I am excited to share a fun and simple project that I recently worked on: an Advice Generator website. This project fetches random pieces of advice from an external API and displays them on a webpage. It's a great way to practice working with APIs and building interactive web applications.

Project Overview

The Advice Generator website is a straightforward application that allows users to get random advice at the click of a button. It uses the Advice Slip API to fetch advice and display it on the webpage.

Features

  • Fetches Advice: Retrieves random advice from the Advice Slip API.
  • Displays Advice: Shows the advice along with an advice number.
  • Interactive Button: Users can fetch new advice by clicking a button.

Technologies Used

  • HTML: For the structure of the webpage.
  • CSS: For styling the webpage.
  • JavaScript: For fetching data from the API and updating the DOM.

Project Structure

Here's a quick look at the project structure:

Advice-Generator/
├── index.html
├── style.css
└── script.js

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Advice-Generator.git
    
  2. Open the project directory:

    cd Advice-Generator
    
  3. Run the project:

    • You can either run it on a local server or simply open the index.html file in a web browser.

Usage

  1. Open the website in a web browser.
  2. Click the "Get Advice" button to fetch a new piece of advice.
  3. Enjoy the wisdom!

Code Explanation

HTML

The HTML file contains the basic structure of the webpage, including a button to fetch advice and a section to display the advice.

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
93f0f5c25f18dab9d176bd4f6de5d30e
    1fc2df4564f5324148703df3b6ed50c1
    4f2fb0231f24e8aef524fc9bf9b9874f
    b2386ffb911b14667cb8f0f91ea547a7Advice Generator6e916e0f7d1e588d4f442bf645aedb2f
    468dcf7b0ee61aef03af1a1fbe6725fc
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
    4883ec0eb33c31828b7c767c806e14c7
        4a249f0d628e2318394fd9b75b4636b1Advice Generator473f0a7621bec819994bb5020d29372a
        763cfba416dfc5411b624194ed788e86Click the button to get a piece of advice!94b3e26ee717c64999d7867364b1b4a3
        59863cd1e20719ad0426694918b5bfefGet Advice65281c5ac262bf6d81768915a4a77ac0
    16b28748ea4df4d9c2150843fecfba68
    84cf5d7ad8199c88ca1d921cae010baf2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

CSS

The CSS file styles the webpage to make it visually appealing.

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f0f0;
}

.container {
    text-align: center;
    background: #fff;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

button {
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
    background-color: #007BFF;
    color: #fff;
    border: none;
    border-radius: 5px;
    margin-top: 20px;
}

button:hover {
    background-color: #0056b3;
}

JavaScript

The JavaScript file fetches advice from the API and updates the DOM.

document.getElementById('adviceBtn').addEventListener('click', fetchAdvice);

function fetchAdvice() {
    fetch('https://api.adviceslip.com/advice')
        .then(response => response.json())
        .then(data => {
            document.getElementById('advice').innerText = `Advice #${data.slip.id}: ${data.slip.advice}`;
        })
        .catch(error => {
            console.error('Error fetching advice:', error);
        });
}

Live Demo

You can check out the live demo of the Advice Generator website here.

Conclusion

Building the Advice Generator website was a fun and educational experience. It helped me practice working with APIs and building interactive web applications. I hope you find this project as enjoyable and informative as I did. Feel free to clone the repository and play around with the code. Happy coding!

Credits

  • This project uses the Advice Slip API.

Author

  • Abhishek Gurjar
    • GitHub Profile

The above is the detailed content of Building an Advice Generator Website. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn