-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
58 lines (44 loc) · 1.77 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const randButton = document.querySelector("#getRandom");
const text = document.querySelector("#matthew").textContent;
const displayDiv = document.querySelector("#displayText");
const contextCheck = document.querySelector("#contextCheck");
const greekButton = document.querySelector("#greek");
let number = 0;
let sentence = "";
// Finds the ends of sentences and questions (including dialogue), and adds "@" for easy splitting.
const textToSplit = text.replace(/\. /g, ".@").replace(/\! /g, "!@").replace(/” /g, '”@').replace(/\.’ /g, ".’@").replace(/\? /g, "?@");
const textArray = textToSplit.split("@");
const limit = textArray.length;
//generate random number, determine if start/end quotes are needed, then display sentence.
function checkForQuotes() {
if (sentence.includes("“") && !sentence.includes("”")) {
sentence = sentence + " ...”";
} else if (!sentence.includes("“") && sentence.includes("”")) {
sentence = "“... " + sentence;
}
}
function deliverRandomSentence() {
number = Math.floor(Math.random() * limit);
sentence = textArray[number];
checkForQuotes();
displayDiv.textContent = sentence;
//Animation
displayDiv.classList.add("textEnter");
setTimeout ( function() {
displayDiv.classList.remove("textEnter");
}, 1000);
}
function checkContext() {
number++;
sentence = textArray[number];
checkForQuotes();
displayDiv.textContent = sentence;
}
//Event listeners for button and keypress
randButton.addEventListener("click", deliverRandomSentence);
contextCheck.addEventListener("click", checkContext);
document.addEventListener("keypress", function(e) {
if (e.which === 32) {
deliverRandomSentence();
}
})