Skip to content

some cleanup #2

Merged
merged 1 commit into from
Apr 27, 2022
Merged
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
54 changes: 34 additions & 20 deletions final-project/js/main.js
Original file line number Diff line number Diff line change
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
}
}

}