Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2 from plb18001/help-on-april-27
some cleanup
  • Loading branch information
plb18001 committed Apr 27, 2022
2 parents 488e457 + 93f9b30 commit 60acd24
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions final-project/js/main.js
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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')
Expand All @@ -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
}
}

}

0 comments on commit 60acd24

Please sign in to comment.