-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
121 lines (102 loc) · 3.37 KB
/
solution.js
File metadata and controls
121 lines (102 loc) · 3.37 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
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
// TODO 1: Generate Pin - Done
// TODO 2: Make keypad functional - Done
// TODO 3: Make Notification work - Done
// TODO 4: Make try left work - Done
// TODO 5: Make C (clear) btn work to clear keypad input - Done
// TODO 6: Remove element ( < ) one after another - Done
// selectors
const generatePinBtn = document.querySelector(".generate-btn");
const generatedPinInput = document.querySelector(".generatedPin");
const showKeyPadValue = document.querySelector(".showValue");
const submitBtn = document.querySelector(".submit-btn");
const correctNotification = document.querySelector(".correct-pin");
const wrongNotification = document.querySelector(".wrong-pin");
let tryValue = document.getElementById("tryLeft");
const removeSingleItem = document.getElementById("removeBtn");
// setting try left as chance
let chance = parseInt(tryValue.innerText);
// Hide the notify section
const notifySection = document.querySelector(".notify-section");
function removeDefaultNotification() {
notifySection.style.display = "none";
}
// Generating 4 digits Pin
function generatedPin() {
let randomNumber = Math.floor(Math.random() * 9000 + 1000);
// console.log(randomNumber);
generatedPinInput.value = randomNumber;
}
// making keypad functional
function inputValue(number) {
// showKeyPadValue.value = showKeyPadValue.value + number;
if (generatedPinInput.value == "") {
alert("Please generate a pin first");
} else {
showKeyPadValue.value += number;
}
// implementing clear function
if (number == "C") {
clearKeyPadInput();
}
}
// Check Pin Match
function checkPin() {
const newPin = generatedPinInput.value;
console.log(newPin);
if (newPin === showKeyPadValue.value) {
showCorrectNotification();
} else {
showWrongNotification();
tryLeft();
}
}
// show correct notification & make button green and setting disabled = true
function showCorrectNotification() {
notifySection.style.display = "block";
correctNotification.style.display = "block";
wrongNotification.style.display = "none";
submitBtn.disabled = true;
submitBtn.style.backgroundColor = "green";
generatePinBtn.disabled = true;
generatePinBtn.style.backgroundColor = "green";
}
// show wrong notification
function showWrongNotification() {
notifySection.style.display = "block";
correctNotification.style.display = "none";
wrongNotification.style.display = "block";
}
// Clear all keypad input
function clearKeyPadInput() {
showKeyPadValue.value = "";
}
// try left function
function tryLeft() {
if (chance > 0) {
chance = chance - 1;
}
// console.log(chance);
tryValue.innerText = chance;
// make button red and setting disabled = true
if (chance == 0) {
submitBtn.disabled = true;
submitBtn.style.backgroundColor = "red";
generatePinBtn.disabled = true;
generatePinBtn.style.backgroundColor = "red";
}
}
// removing single item from keypad
function removeSingleElement() {
if (showKeyPadValue !== "") {
let currentValue = showKeyPadValue.value;
// Remove the last character from the currentValue string
showKeyPadValue.value = currentValue.slice(0, -1);
}
}
// check pin on submit click
submitBtn.addEventListener("click", checkPin);
// fire the generate pin function
generatePinBtn.addEventListener("click", generatedPin);
// remove default notification
removeDefaultNotification();
removeSingleItem.addEventListener("click", removeSingleElement);