-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (55 loc) · 2.07 KB
/
Copy pathscript.js
File metadata and controls
65 lines (55 loc) · 2.07 KB
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
59
60
61
62
63
64
65
const LINES = sourceLinePairs.length;
let button = document.querySelector("button");
let sonnet = document.querySelector("#sonnet");
(function createLines() {
for (let i = 0; i < 14; i++) {
// li Node
let item = document.createElement("li");
item.classList.add("line");
sonnet.appendChild(item);
// Line Count Node
lineNumber = Array.from(sonnet.children).filter((el) => el.tagName === "LI")
.length;
let lineCount = document.createElement("span");
lineCount.classList.add("line-count")
lineCount.textContent = lineNumber;
// Text Node
let text = document.createElement("span");
text.classList.add("line-text");
// Source Info node
let soureceInfo = document.createElement("span");
soureceInfo.classList.add("source-info");
//Append all nodes
sonnet.lastChild.appendChild(lineCount);
sonnet.lastChild.appendChild(text);
sonnet.lastChild.appendChild(soureceInfo);
}
createPoem();
})();
function createPoem() {
let sonnetLines = Array.from(sonnet.children);
let linePairs = Array.from({ length: 7 }, () => getRandomInt(LINES - 1)).map(
(el) => sourceLinePairs[el]
);
// There's probably a cleaner/DRYer way to assign these
sonnetLines.map((element, index) => {
if (index % 4 < 2 && index !== 13) {
let linePair = linePairs[Math.ceil(index / 2)];
element.children[1].textContent = linePair.lineOne.text;
element.children[2].textContent = `${linePair.SonnetNumber}:${linePair.lineOne.number}`;
} else if (index % 4 > 1) {
let linePair = linePairs[Math.ceil(index / 2) - 1];
element.children[1].textContent = linePair.lineTwo.text;
element.children[2].textContent = `${linePair.SonnetNumber}:${linePair.lineTwo.number}`;
} else {
element.children[1].textContent = linePairs[6].lineTwo.text;
element.children[2].textContent = `${linePairs[6].SonnetNumber}: ${linePairs[6].lineTwo.number}`;
}
});
}
button.addEventListener("click", () => {
createPoem();
});
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}