-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
144 lines (123 loc) · 4.65 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SpreadPicker</title>
<style>
table,
td {
border: 1px solid #333;
}
thead,
tfoot {
background-color: #333;
color: #fff;
}
</style>
</head>
<body>
<h1>hello!</h1>
<form action="#" id="submitGame">
<input type=" text" name="firstTeam" id="firstTeam" placeholder="firstTeam">
<input type="text" name="secondTeam" id="secondTeam" placeholder="secondTeam">
<input type="number" name="week" id="week" placeholder="week #">
<input type="number" name="spread" id="spread" placeholder="spread">
<input type="submit" />
</form>
<h2 id="sumbitted"></h2>
<table>
<thead>
<tr>
<th>Team One</th>
<th>Team Two</th>
<th>Week #</th>
<th>Spread</th>
<th>Winner</th>
</tr>
</thead>
<tbody id="whatever">
</tbody>
</table>
<script>
// alert('hello')
// document.getElementById("teamNameSubmit").addEventListener("click", ()=>alert('Ive been clicked'))
let games = []
class game {
constructor(firstTeam, secondTeam, week, spread) {
this.firstTeam = firstTeam
this.secondTeam = secondTeam
this.week = week
this.choice = null
this.spread = spread
this.firstTeamScore = null
this.secondTeamScore = null
}
chooseTeam(team) {
this.choice = team
}
checkCorrect() {
if (this.choice != null, this.firstTeamScore != null, this.secondTeamScore != null, this.spread) {
return calculateSpread(this.firstTeamScore, this.secondTeamScore, this.choice, this.spread)
}
}
}
document.getElementById('submitGame').addEventListener('submit', submitGame)
function submitGame(event) {
event.preventDefault();
console.log("here")
let firstTeam = document.getElementById("firstTeam").value
let secondTeam = document.getElementById("secondTeam").value
let week = document.getElementById("week").value
let spread = document.getElementById("spread").value
let gameObject = new game(firstTeam, secondTeam, week, spread)
games.push(gameObject)
console.log(games)
buildGameList()
}
function buildGameList() {
let table = ''
games.forEach((game, gameIndex) => {
table += `
<tr>
<td>
<button ${game.choice == 1 ? 'style="background-color:green"' : ''} onclick='selectTeam(${gameIndex},1)'>${game.firstTeam}</button>
<input type="number" oninput="updateTeamScore(${gameIndex},1,this.value)" value="${game.firstTeamScore}">
</td>
<td>
<button ${game.choice == 2 ? 'style="background-color:red"' : ''} onclick='selectTeam(${gameIndex},2)'>${game.secondTeam}</button>
<input type="number" oninput="updateTeamScore(${gameIndex},2,this.value)" value="${game.secondTeamScore}">
</td>
<td>${game.week}</td>
<td>${game.spread}</td>
<td>${game.checkCorrect()}</td>
</tr>
`
});
document.getElementById("whatever").innerHTML = table
}
function selectTeam(index, team) {
games[index].chooseTeam(team)
buildGameList()
}
function updateTeamScore(index, team, score) {
if (team === 1) {
games[index].firstTeamScore = score
} else {
games[index].secondTeamScore = score
}
console.log(games)
}
function calculateSpread(firstScore = 10, secondScore = 7, spread = 2, ChoseFirstTeam = true) {
// team1 score 20 spread 6.5 team2 score 35
let spreadResult = firstScore - secondScore
console.log(firstScore, secondScore, spread, ChoseFirstTeam)
if (spreadResult >= spread && ChoseFirstTeam) {
return `<span>✓</span>`
}
return `<span>X</span>`
}
</script>
</body>
</html>