-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
116 lines (97 loc) · 3.38 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
function addPlayer()
{
function isInputEmpty(inputField)
{
return inputField.value === '';
}
function resetInput(inputField)
{
inputField.value = '';
}
const playerHolder = document.querySelector('.player_holder');
const inputField = document.querySelector('.input_field');
let playerName = inputField.value;
inputField.focus();
// https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
if (isInputEmpty(inputField)) return;
let newP = document.createElement('p');
newP.textContent = playerName;
playerHolder.appendChild(newP);
resetInput(inputField);
}
function choosePlayer()
{
function isListEmpty(playerArray)
{
// https://stackoverflow.com/questions/11743392/check-if-an-array-is-empty-or-exists
return playerArray.length === 0;
}
function choose(choices)
{
// https://stackoverflow.com/questions/9071573/is-there-a-simple-way-to-make-a-random-selection-from-an-array-in-javascript-or
let index = Math.floor(Math.random() * choices.length);
return choices[index];
}
const playerArray = document.querySelectorAll('div.player_holder p');
if (isListEmpty(playerArray)) return;
showWinner(choose(playerArray).textContent) //alert(choose(playerArray).textContent); // change alert to something else -> showWinner()
clearPlayerList()
}
function checkEnterPress(e)
{
function isInputEmpty(inputField)
{
return inputField.value === '';
}
const inputField = document.querySelector('.input_field');
if (e.key === 'Enter')
{
if (isInputEmpty(inputField)) choosePlayer(); else addPlayer();
}
}
function showWinner(winner)
{
function removeAlertBox()
{
const alertBoxContainer = document.querySelector('.alert_box_container');
alertBoxContainer.remove();
}
//Do stuff after, maybe show a div with a shadow and such?
const body = document.querySelector('body');
let alertBoxContainer = document.createElement('div');
alertBoxContainer.className = 'alert_box_container';
let alertBox = document.createElement('div');
alertBox.className = 'alert_box';
alertBox.textContent = 'And the first player is'
alertBoxContainer.appendChild(alertBox)
body.appendChild(alertBoxContainer);
t = 500 // Time variable
for (const element of ['.','.','.',`\r\n${winner}`]){
window.setTimeout(function(){alertBox.textContent += element;}, t);
t += 500
// https://stackoverflow.com/questions/9980416/how-can-i-insert-new-line-carriage-returns-into-an-element-textcontent
}
window.setTimeout(function(){alertBox.textContent += '\r\nClick anywhere to reset';},t) // I really hate timeout in JS
alertBoxContainer.addEventListener('click', removeAlertBox);
}
function sleep(ms)
{
return new Promise(resolve => setTimeout(resolve, ms));
}
function clearPlayerList()
{
// https://stackoverflow.com/questions/3955229/remove-all-child-elements-of-a-dom-node-in-javascript
const playerHolder = document.querySelector('.player_holder');
playerHolder.textContent = '';
}
function main()
{
const inputField = document.querySelector('.input_field');
const buttonAdd = document.querySelector('.button_add');
const buttonChoose = document.querySelector('.button_choose');
inputField.focus();
inputField.addEventListener('keydown', checkEnterPress);
buttonAdd.addEventListener('click', addPlayer);
buttonChoose.addEventListener('click', choosePlayer);
}
main()