diff --git a/Sprint-3/quote-generator/index.html b/Sprint-3/quote-generator/index.html index 30b434bcf..befe8788e 100644 --- a/Sprint-3/quote-generator/index.html +++ b/Sprint-3/quote-generator/index.html @@ -1,9 +1,9 @@ - + - Title here + Quote generator app diff --git a/Sprint-3/quote-generator/package.json b/Sprint-3/quote-generator/package.json index 0f6f98917..3f5126ba6 100644 --- a/Sprint-3/quote-generator/package.json +++ b/Sprint-3/quote-generator/package.json @@ -2,7 +2,7 @@ "name": "quote-generator", "version": "1.0.0", "license": "CC-BY-SA-4.0", - "description": "You must update this package", + "description": "Simple quote generator that picks a quote at random", "scripts": { "test": "jest --config=../jest.config.js quote-generator" }, diff --git a/Sprint-3/quote-generator/quotes.js b/Sprint-3/quote-generator/quotes.js index 4a4d04b72..1212e2152 100644 --- a/Sprint-3/quote-generator/quotes.js +++ b/Sprint-3/quote-generator/quotes.js @@ -1,5 +1,38 @@ // DO NOT EDIT BELOW HERE +document.addEventListener("DOMContentLoaded", () => { + const quoteEl = document.getElementById("quote"); + const authorEl = document.getElementById("author"); + const newQuoteBtn = document.getElementById("new-quote"); + if (!quoteEl || !authorEl || !newQuoteBtn) { + console.warn("Missing #quote, #author or #new-quote element."); + return; + } + let lastIndex = -1; + + function showRandomQuote() { + let index; + + if (quotes.length === 1) { + index = 0; + } else { + do { + index = Math.floor(Math.random() * quotes.length); + } while (index === lastIndex); + } + + lastIndex = index; + + const chosen = quotes[index]; + + quoteEl.textContent = `"${chosen.quote}"`; + authorEl.textContent = `— ${chosen.author}`; + } + + showRandomQuote(); + + newQuoteBtn.addEventListener("click", showRandomQuote); +}); // pickFromArray is a function which will return one item, at // random, from the given array. //