-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
153 lines (137 loc) · 4.65 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Helper method to sum total for each color
*/
const sumTotal = (arr) => {
let total = 0;
Array.from(arr).forEach((input) => {
total += Number(input.value)
})
return total
}
const rowIsValid = (inputs) => {
const values = []
Array.from(inputs).forEach((input) => {
values.push(input.value)
})
if (JSON.stringify(values.sort()) != JSON.stringify(["1", "2", "3", "4"])) {
alert("Make sure each value (1,2,3,4) only occurs once per row")
return false
}
return true
}
/**
* Helper method to validate each number only once per row
*/
const rowsAreValid = () => {
const rows = document.getElementsByTagName("fieldset")
let invalidRowsFound = 0
Array.from(rows).forEach((row) => {
if (!rowIsValid(row.elements)) {
invalidRowsFound += 1
}
})
if (invalidRowsFound > 0) {
return false
}
return true
}
/**
* Store array of ranking choices so that we can persist user input across page reloads on the index page
*/
const storeRawResults = () => {
const valuesCollection = document.getElementById("true-colors").getElementsByTagName("input")
const resultsToStore = []
Array.from(valuesCollection).forEach((input) => {
if(input.type === "text") {
resultsToStore.push(input.value)
}
});
sessionStorage.setItem("results", JSON.stringify({"arr": resultsToStore}))
}
/**
* Store total for each color for access on success page
*/
const storeTotal = () => {
const oranges = document.getElementsByClassName("orange")
const blues = document.getElementsByClassName("blue")
const golds = document.getElementsByClassName("gold")
const greens = document.getElementsByClassName("green")
const orangeTotal = sumTotal(oranges)
const blueTotal = sumTotal(blues)
const goldTotal = sumTotal(golds)
const greenTotal = sumTotal(greens)
sessionStorage.setItem("orangeTotal", orangeTotal)
sessionStorage.setItem("blueTotal", blueTotal)
sessionStorage.setItem("goldTotal", goldTotal)
sessionStorage.setItem("greenTotal", greenTotal)
}
/**
* On submit from index page do the following:
*/
const onSubmit = () => {
if (!rowsAreValid()) {
return false
}
// TODO: Could clean up data structures and only store once
storeRawResults()
storeTotal()
}
/**
* On page load of index page, try and access values from session if any
*/
const accessSessionValues = () => {
const valuesObj = sessionStorage.getItem("results")
if (valuesObj != undefined) {
const valuesArr = JSON.parse(valuesObj)["arr"]
const inputs = document.getElementById("true-colors").getElementsByTagName("input")
Array.from(inputs).forEach((input, i) => {
if(input.type === "text") {
if (valuesArr[i] != undefined) {
input.value = valuesArr[i]
}
}
});
}
}
/**
* Helper to display an for orange (only vowel possibility)
*/
const aOrAn = (colorStr) => {
if (colorStr[0] === 'o') {
return "an"
}
return "a"
}
/**
* On success page load, access totals and inject into page to display
*/
const setResults = () => {
const heading = document.getElementById('results-heading')
if (sessionStorage.length === 0) {
heading.innerHTML = "Take the <a href='index.html'>true colors quiz<a> to find out your results"
return
}
const results = []
results.push(["orange", sessionStorage.getItem("orangeTotal")])
results.push(["blue", sessionStorage.getItem("blueTotal")])
results.push(["gold", sessionStorage.getItem("goldTotal")])
results.push(["green", sessionStorage.getItem("greenTotal")])
results.sort((a, b) => {
return Number(b[1]) - Number(a[1])
})
const [topColor, topScore] = results[0]
const [secondColor, secondScore] = results[1]
if (topScore === secondScore) {
heading.innerHTML = `You are ${aOrAn(topColor)} <span id=${topColor}>${topColor}</span> and ${aOrAn(secondColor)} <span id=${secondColor}>${secondColor}</span> primary. A tie! 🦄`
} else {
heading.innerHTML = `You are ${aOrAn(topColor)} <span id=${topColor}>${topColor}</span> primary with ${aOrAn(secondColor)} <span id=${secondColor}>${secondColor}</span> secondary 🦄`
}
const listContainer = document.getElementById('results-list')
results.forEach((item) => {
const [color, score] = item
const listItem = document.createElement("li")
listItem.innerHTML = `Your total ${color} score is: ${score}`
listItem.id = `${color}-total`
listContainer.appendChild(listItem)
})
}