-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkonami.html
More file actions
79 lines (66 loc) · 2.9 KB
/
konami.html
File metadata and controls
79 lines (66 loc) · 2.9 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>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Konami Code</title>
</head>
<body>
<h1>Konami Code</h1>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
"use strict";
/*
Steps:
1. Keep track of what the user is typing
2. Use the jquery keyup event which is called every time the user presses a key
3. Use a variable that stores
4. Clear input whenever user types something invalid
5. To check if the input is invalid
*/
const correctInput = "3838404037393739666513";
var currentInput = "";
// create an empty variable to keep track of user inputs
// calls event listener whenever user presses a key
$(document).keyup(function(event){
// since this is a keyboard event we can use the keycode property of the event to check which key the user typed
// update user input every time a key is pressed
currentInput += event.keyCode;
console.log(currentInput);
// check if currentInput has any characters that are not in the correctInput
// check if every character in the currentInput is in the correctInput
// input is valid if the correctInput includes all of the currentInput
// valid is a boolean
const valid = correctInput.includes(currentInput);
// input is correct if correctInput matches the currentInput
const correct = correctInput === currentInput;
if (correct) {
alert("You have added 30 lives!");
}
if (!valid) {
alert("Invalid code. Start over.");
// set input back to empty so user can start typing the code again
currentInput = "";
}
$("#input").html(currentInput);
});
//Albert's solution:
// $(document).ready(function() {
// var code = [];
// var konamiCode = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13];
// $(document).keyup(function() {
// code.push(event.keyCode);
// if (code.slice(code.length - 11, code.length) === konamiCode) {
// alert("You now have 30 extra lives!")
// }
// });
// });
</script>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>