Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new project Random Quote generator #1827

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 126 - Random Quote Generator/font/sans.ttf
Binary file not shown.
34 changes: 34 additions & 0 deletions 126 - Random Quote Generator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<link rel="shortcut icon" href="assets/favicon.png" type="image/x-icon">
<title>Random Quote Generator</title>
</head>

<body>
<div class="container">
<h2>Random Quote Generator</h2>
<div id="quote">Loading...</div>
<div id="btn" onclick="getQuote();">Generate Quote</div>
<div id="copy-btn" onclick="copyQuote();">Copy Quote</div>
</div>

<footer>
<p>&#x3c; &#47; &#x3e; with ❀️ by
<a href="https://swapnilsparsh.github.io/" class="name"><u>Swapnil Srivastava</u></a>
<br>
<a href="https://github.com/swapnilsparsh/30DaysOfJavaScript" target="_blank"
class="ProjectName">#30DaysOfJavaScript
</a>
</p>
</footer>

<script src="main.js"></script>
</body>

</html>
45 changes: 45 additions & 0 deletions 126 - Random Quote Generator/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
document.addEventListener("DOMContentLoaded", () => {
let quotes = [];

// Fetch quotes from the API
fetch("https://type.fit/api/quotes")
.then((response) => response.json())
.then((data) => {
quotes = data;
getQuote(); // Display an initial quote
});

// Function to generate a new quote
function getQuote() {
if (quotes.length === 0) {
return;
}
const randomIndex = Math.floor(Math.random() * quotes.length);
const quoteElement = document.getElementById("quote");
const quoteText = quotes[randomIndex].text;
const quoteAuthor = cleanAuthor(quotes[randomIndex].author || "Unknown");
quoteElement.textContent = `${quoteText} - ${quoteAuthor}`;
}

// Function to clean up the author text
function cleanAuthor(author) {
// Remove unwanted text patterns (e.g., ", type.fit")
return author.replace(/, type\.fit/g, "");
}

// Function to copy the current quote to the clipboard
function copyQuote() {
const quoteElement = document.getElementById("quote");
const textArea = document.createElement("textarea");
textArea.value = quoteElement.textContent;
document.body.appendChild(textArea);
textArea.select();
document.execCommand("copy");
document.body.removeChild(textArea);
alert("Quote copied to clipboard!");
}

// Expose functions to the global scope so they can be called from the onclick attribute
window.getQuote = getQuote;
window.copyQuote = copyQuote;
});
78 changes: 78 additions & 0 deletions 126 - Random Quote Generator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
* {
padding: 0;
margin: 0;
font-family: "sans";
user-select: none;
}

@font-face {
src: url("font/sans.ttf");
font-family: "sans";
}

.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
background: #19172e;
}

h2 {
color: white;
font-size: 2rem;
text-align: center;
margin-bottom: 20px;
}

#quote {
color: rgb(15, 201, 233);
font-size: 1.5rem;
text-align: center;
margin-bottom: 20px;
}

#btn,
#copy-btn {
cursor: pointer;
color: #fff;
background: #333;
font-size: 1rem;
padding: 10px 20px;
border-radius: 5px;
transition: background 0.3s ease;
text-align: center;
margin-top: 20px;
}

#btn:hover,
#copy-btn:hover {
background: #1e8e3e;
}

footer {
text-align: center;
color: white;
font-size: 1rem;
position: fixed;
bottom: 0;
left: 0;
right: 0;
padding: 5px;
line-height: 3vh;
}

footer a:visited {
color: inherit;
}

.name {
text-decoration: none;
color: white;
}

.ProjectName {
color: white;
text-decoration: none;
}
Binary file added 30DaysOfJavaScript/assets/126 - quote.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading