-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkonami.html
More file actions
79 lines (69 loc) · 2.5 KB
/
konami.html
File metadata and controls
79 lines (69 loc) · 2.5 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html lang="en">
<head>
<title>Konami Code</title>
</head>
<body>
<h1>Konami Code</h1>
<script>
"use strict";
// // document.addEventListener("keyup", event => console.log(event.key) );
//
// document.addEventListener("keydown", event => console.log(event.key) );
// document.addEventListener('keydown', event => userKeys.push(`key pressed: ${event.target}`))
// document.onkeydown = function(e) {
// let keyPress;
// if (typeof event !== 'undefined') {
// keyPress = event.key;
// }
// else if (e) {
// keyPress = e.which;
// }
// userKeys.push(String.fromCharCode(keyPress));
//
// };
// let userKeys = [];
// const konami = "['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight', 'b', 'a', 'Enter']";
// // // document.addEventListener('keydown', event => userKeys.push(`key pressed: ${event.target}`))
// //
// // document.onkeydown = function myKeyPressFunction(event) {
// // userKeys.push(event.key);
// //
// // if (`${userKeys} === ${konami}`) {
// // console.log(userKeys);
// //
// // } else{
// // console.log("yep")
// // }}
// //
// // // document.addEventListener('keydown', myKeyPressFunction);}
let userKeys = [];
const konamiCode = ["ArrowUp", "ArrowUp", "ArrowDown", "ArrowDown", "ArrowLeft", "ArrowRight", "ArrowLeft", "ArrowRight", "b", "a", "Enter"];
function checkCode(e) {
userKeys.push(e.key);
if (userKeys.length === konamiCode.length && userKeys.every((value, index) => value === konamiCode[index])) {
alert("You have added 30 lives!");
userKeys = [];
}
}
document.addEventListener("keydown", checkCode);
// ===================
// JOSH'S CODE IS STRUCTURED DIFFERENTLY FROM MINE AND HAS AN ALERT FOR TRYING AGAIN
// const konamiCode = ["ArrowUp", "ArrowUp", "ArrowDown", "ArrowDown", "ArrowLeft", "ArrowRight", "ArrowLeft", "ArrowRight", "b", "a", "Enter"];
// let userInput = 0;
//
// document.addEventListener("keyup", event => {
// if (konamiCode[userInput] !== event.key) {
// userInput = 0;
// alert("Wrong! Try again!")
// } else {
// userInput++;
// }
// if (userInput === 11) {
// alert("You get 30 lives!");
// userInput = 0;
// }
// });
</script>
</body>
</html>