Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
4:34 pm
  • Loading branch information
plb18001 committed Apr 26, 2022
1 parent db4c2c1 commit 488e457
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 37 deletions.
14 changes: 14 additions & 0 deletions final-project/css/main.css
Expand Up @@ -134,11 +134,25 @@ body {
}

#quoteDisplay {
margin-left: calc(1rem + 2px);
margin-right: calc(1rem + 2px);
/* -webkit-user-select: none;
-webkit-touch-callout: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none; */
}
#authorDisplay {
margin-bottom: 1rem;
margin-left: calc(1rem + 2px);
margin-right: calc(1rem + 2px);
display: flex;
justify-content: flex-end;
font-style: italic;
font-weight: bold;
}


#nextButton {
margin-top: 1rem;
}
Expand Down
16 changes: 5 additions & 11 deletions final-project/index.html
@@ -1,10 +1,10 @@
<!doctype html>
<html class="no-js" lang="">
<html class="no-js" lang="zxx">

<head>
<meta charset="utf-8">
<title></title>
<meta name="description" content="">
<title>3475 Final Project</title>
<meta name="description" content="Paolo's Speed Typing Game">
<meta name="viewport" content="width=device-width, initial-scale=1">

<meta property="og:title" content="">
Expand All @@ -28,6 +28,8 @@

<div class="container">
<div id="quoteDisplay"></div>
<div id="authorDisplay"></div>
<div id="wpm"></div>
<textarea id="quoteInput" autofocus></textarea>
</div>

Expand All @@ -40,17 +42,9 @@



<!-- Add your site or application content here -->
<script src="js/vendor/modernizr-3.11.2.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>

<!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
<script>
window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
ga('create', 'UA-XXXXX-Y', 'auto'); ga('set', 'anonymizeIp', true); ga('set', 'transport', 'beacon'); ga('send', 'pageview')
</script>
<script src="https://www.google-analytics.com/analytics.js" async></script>
</body>

</html>
141 changes: 115 additions & 26 deletions final-project/js/main.js
@@ -1,54 +1,143 @@
const quote_api = 'https://api.quotable.io/random'
const quote_api = 'https://api.quotable.io/random?minLength=160'
const quoteDisplay = document.querySelector('#quoteDisplay')
const authorDisplay = document.querySelector('#authorDisplay')
const quoteInput = document.querySelector('#quoteInput')
const nextButton = document.querySelector('#nextButton')
const incorrectEntry = document.querySelector('#incorrectEntry')
const timer = document.querySelector('#timer')
const wpm = document.querySelector('#wpm')
let timerStart = false;
let startTime
let count = 5

// Use this to flag typing errors
let typingError = false

// Key presses in this array will be ignored
const badKeys = [
"Enter",
"Delete"
]


quoteInput.addEventListener('keydown', (event) => {

// Disable "bad" keys
if(badKeys.indexOf(event.key) !== -1){
console.error('"' + event.key + '" key is not allowed. Ignoring input.')
event.preventDefault();
}

// Or if there's a mistake
if(typingError === true && event.key !== 'Backspace'){
// Disable further typing
event.preventDefault();
}

quoteInput.addEventListener('input', () => {
const arrayQuote = quoteDisplay.querySelectorAll('span')
const arrayValue = quoteInput.value.split('')

arrayQuote.forEach((charSpan, index) => {
const character = arrayValue[index]
if (character == null) {
charSpan.classList.remove('correct')
charSpan.classList.remove('incorrect')
} else if (character === charSpan.innerText) {
charSpan.classList.add('correct')
charSpan.classList.remove('incorrect')
} else {
charSpan.classList.add('incorrect')
charSpan.classList.remove('correct')

// This stops after you mess up and allows you to backspace but not continue typing after fixing your mistake
// if (quoteInput.keyCode !== 13){ quoteInput.setAttribute('maxlength', 0)}
// else{ quoteInput.setAttribute('maxlength', 100)}
}
})
})

function dontType(event){
event.preventDefault()
}
quoteInput.addEventListener('input', (event) => {
console.log(event)

const arrayQuote = quoteDisplay.querySelectorAll('span')
const arrayValue = quoteInput.value.split('')
let finished = true;
arrayQuote.forEach((charSpan, index) => {
if(typingError !== true || event.inputType == 'deleteContentBackward'){
const character = arrayValue[index]
if (character == null) {
charSpan.classList.remove('correct')
charSpan.classList.remove('incorrect')
typingError = false;
finished = false;
} else if (character === charSpan.innerText) {
charSpan.classList.add('correct')
charSpan.classList.remove('incorrect')
typingError = false;
} else {
charSpan.classList.add('incorrect')
charSpan.classList.remove('correct')
typingError = true
finished = false;
console.log('error', typingError, window.typingError)
}
}
})
if(finished) {
quoteInput.readOnly = true;
timerStart = false;
}
})


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)
}

async function getNextQuote () {
const quote = await getRandomQuote()
const authorName = await getAuthorName()
quoteDisplay.innerHTML = ''
quote.split('').forEach(character => {
const charSpan = document.createElement('span')
charSpan.innerText = character
quoteDisplay.appendChild(charSpan)
})
authorDisplay.innerHTML = " -" + authorName
quoteInput.value = null;
timerStart = true;
count = 5;
startTimer()
}


function startTimer() {
timer.innerText = 0
startTime = new Date()
setInterval(() =>{
if (timerStart) {
if (count > 0) {
console.log(count);
count--;
}
else {
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
wpm.innerText = calcWpm
return timer.innerText = timer.innerText
}
}

}

getNextQuote()

0 comments on commit 488e457

Please sign in to comment.