about summary refs log tree commit diff
path: root/script.js
blob: ea4bd12c46d58e8ef3df14873fa0a3e641136dc7 (plain)
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
class Wednesdayle {
	constructor() {
		this.active = true;
		this.nextBox = 0;
		this.boxes = new Array();
		for (let i = 0; i < 9; ++i) {
			this.boxes.push(document.getElementById('box' + i));
		}

		this.buttons = new Array();
		for (let i = 0; i < 7; ++i) {
			let btn = document.getElementById('key' + i);
			btn.addEventListener('click', this.buttonPush.bind(this));
			this.buttons.push(btn);
		}

		window.addEventListener('keydown', this.keydown.bind(this));

		this.delete = document.getElementById('delete');
		this.delete.addEventListener('click', this.letterUndid.bind(this));

		this.enter = document.getElementById('enter');
		this.enter.addEventListener('click', this.submit.bind(this));

		this.dropdown = document.getElementById('dropdown');
		document.getElementById('close-dropdown').addEventListener('click', this.closeDropdown.bind(this));

		this.theword = document.getElementById('theword');
		this.notword = document.getElementById('notword');
	}

	// Terrible function name genny
	letterDid(letter) {
		if (!this.active) {
			return;
		}

		if (this.nextBox < this.boxes.length) {
			let up = letter.toUpperCase();

			this.boxes[this.nextBox].innerText = up;
			if (this.nextBox < this.boxes.length) {
				this.nextBox++;
			}
		}
	}

	// undid? really? genny,,,
	letterUndid() {
		if (!this.active) {
			return;
		}

		if (this.nextBox > 0) {
			this.nextBox--;
			this.boxes[this.nextBox].innerText = '';
		}
	}

	submit() {
		if (!this.active) {
			return;
		}

		let word = "wednesday";
		for (let i = 0; i < this.boxes.length; ++i) {
			if (word[i].toUpperCase() != this.boxes[i].innerText.toUpperCase()) {
				this.notword.style.display = "block";
				this.active = false;
				return;
			}
		}

		this.theword.style.display = "block";
		this.active = false;
	}

	buttonPush(event) {
		if (!this.active) {
			return;
		}

		let btn = event.target;
		let letter = btn.innerText;

		this.letterDid(letter);
	}

	keydown(event) {
		if (!this.active) {
			return;
		}

		let allowed = "weyadsn";
		let key = event.key;

		if (allowed.includes(key)) {
			this.letterDid(key);
		} else if (key == "Enter") {
			this.submit();
		} else if (key == "Backspace") {
			this.letterUndid();
		}
	}

	closeDropdown() {
		console.log('thf');
		this.dropdown.style.display = "none";
	}
}

let wednesdayle;

function setup() {
	console.log("started");
	wednesdayle = new Wednesdayle();
}

window.addEventListener('DOMContentLoaded', setup)