diff --git a/final-project/js/main.js b/final-project/js/main.js index b95c898..d47f59b 100644 --- a/final-project/js/main.js +++ b/final-project/js/main.js @@ -10,6 +10,9 @@ let timerStart = false; let startTime let count = 5 +// Current quote information, as globals +let quote, authorName, quoteLength; + // Use this to flag typing errors let typingError = false @@ -38,7 +41,7 @@ quoteInput.addEventListener('keydown', (event) => { quoteInput.addEventListener('input', (event) => { console.log(event) - + const arrayQuote = quoteDisplay.querySelectorAll('span') const arrayValue = quoteInput.value.split('') let finished = true; @@ -74,27 +77,36 @@ nextButton.addEventListener('click', () => { getNextQuote() quoteInput.readOnly = false; quoteInput.focus(); -}) +}) function getRandomQuote() { return fetch(quote_api) .then(response => response.json()) - .then(data => data.content) -} -function getAuthorName(){ - return fetch(quote_api) - .then(response => response.json()) - .then(data => data.author) -} -function getQuoteLength(){ - return fetch(quote_api) - .then(response => response.json()) - .then(data => data.length) + .then(data => data) + .catch(error => { + console.log(error) + }) } +// These functions are redundant. We can get everything from `getRandomQuote` +// +// function getAuthorName(){ +// return fetch(quote_api) +// .then(response => response.json()) +// .then(data => data.author) +// } +// function getQuoteLength(){ +// return fetch(quote_api) +// .then(response => response.json()) +// .then(data => data.length) +// } + async function getNextQuote () { - const quote = await getRandomQuote() - const authorName = await getAuthorName() + // Gets the quote, author, and length + // See for info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#object_destructuring + ({ content: quote, author: authorName, length: quoteLength } = await getRandomQuote()) + console.log(quote, authorName, quoteLength) + quoteDisplay.innerHTML = '' quote.split('').forEach(character => { const charSpan = document.createElement('span') @@ -112,32 +124,34 @@ async function getNextQuote () { function startTimer() { timer.innerText = 0 startTime = new Date() - setInterval(() =>{ + + let interval = setInterval(() =>{ if (timerStart) { if (count > 0) { console.log(count); count--; } else { + clearInterval(interval) + console.log('RUnning else on startTimer() interval') setInterval(() => { timer.innerText = getTimerTime() }, 100); } } }, 1000); - + } function getTimerTime() { if (timerStart){ if (count > 0){ return (new Date() - startTime) / 1000 }else { - const getLength = getQuoteLength() - let calcWpm = (getLength / 5) / timer.innerText + let calcWpm = (quoteLength / 5) / timer.innerText wpm.innerText = calcWpm return timer.innerText = timer.innerText } } - + }