Skip to content

Commit

Permalink
Merge pull request #1 from mazayastudio/dev
Browse files Browse the repository at this point in the history
Optimize Code
  • Loading branch information
mazayastudio committed Dec 12, 2023
2 parents bfdc7a8 + fa803ad commit ddacf3f
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ const newQuoteBtn = document.getElementById("new-quote");
const loader = document.getElementById('loader');
let apiQuotes = [];

// Show loading
function loading() {
function showLoadingSpinner() {
loader.hidden = false;
quoteContainer.hidden = true;
}

// Hide loading
function complete() {
function removeLoadingSpinner() {
quoteContainer.hidden = false;
loader.hidden = true;
}

// Show new quote
function newQuote() {
loading();
showLoadingSpinner();
// Pick a random quote from apiQuotes array
const quote = apiQuotes[Math.floor(Math.random() * apiQuotes.length)];
// check if author field is blank and replace it with 'unknown'
Expand All @@ -37,19 +35,19 @@ function newQuote() {
}
// set quote, hide loader
quoteText.textContent = quote.text;
complete();
removeLoadingSpinner();
}

// Get quotes from API
async function getQuotes() {
loading();
showLoadingSpinner();
const apiUrl = "https://jacintodesign.github.io/quotes-api/data/quotes.json";
try {
const response = await fetch(apiUrl);
apiQuotes = await response.json();
newQuote();
} catch (error) {
// catch error
console.error;
}
}

Expand Down

0 comments on commit ddacf3f

Please sign in to comment.