-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.js
174 lines (159 loc) · 6.51 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Assigning variables to HTML element.
const activeUserEl = document.getElementsByClassName("active-account")[0];
const activeUserBalanceEl = document.getElementsByClassName("total-money")[0];
const balanceInputEl = document.getElementsByClassName("money-input")[0];
const depositTextEl = document.getElementById("deposit-text");
const withdrawTextEl = document.getElementById("withdraw-text");
const accounts = [];
let activeUser = null;
function createAccount() {
let name = prompt("Enter your account name.").trim(); // " js ".trim() -> "js"
let password = prompt(
"Enter your password.\n* You password must be minimum 8 characters long. \n Your password can not include speacial characters (/!@$#?*-_ etc.) \n* Your password must include\n- at least one uppercase letter\n- at least one lowercase letter\n- at least one number.\n* Your password must be minimum 8 characters long."
);
if (name === "" || password === "") {
// Checking if name or password is empty.
alert("Account name or password can not be empty.");
return;
}
for (let i = 0; i < accounts.length; i++) {
// Checking if account name is already taken.
if (accounts[i].name === name) {
alert("That account name has been using already.");
return;
}
}
// Password validation - Coded by @eneskaya12
if (password.length < 8) {
// Check if it is less than 8 characters.
alert("Error: Your password must be minimum 8 characters long!");
return;
} else if (/[^A-Za-z0-9]/.test(password)) {
// Check if it includes special characters.
alert(
"Error: Your password can not include speacial characters (/!@$#?*-_ etc.)!"
);
return;
} else if (!/[A-Z]/.test(password)) {
// Check if it includes at least one uppercase letter.
alert(
"Error: Your password must include at least one uppercase character!"
);
return;
} else if (!/[a-z]/.test(password)) {
// Check if it includes at least one lowercase letter.
alert(
"Error: Your password must include at least one lowercase character!"
);
return;
} else if (!/[0-9]/.test(password)) {
// Check if it includes at least one number.
alert("Error: Your password must include at least one number!");
return;
}
let newAccount = {
// Creating new account object.
id: accounts.length + 1, // Assigning id automatically.
name: name,
password: password,
balance: 0,
};
accounts.push(newAccount); // Adding new account to accounts array.
alert("Account has been added successfully!"); // Alerting user that account has been created.
}
function changeAccount() {
// Change account function.
let name = prompt("Please enter your account name.");
let password = prompt("Please enter your password.");
let isUserFound = false; // Assigning false value to isUserFound variable. It will be used later.
for (let i = 0; i < accounts.length; i++) {
if (name === accounts[i].name && password === accounts[i].password) {
// Checking if user name and password is correct.
isUserFound = true; // If user name and password is correct, isUserFound variable will be true. User has been found.
activeUser = accounts[i]; // Assigning activeUser variable to accounts[i] object.
updateAccount(activeUser); // Updating account information.
alert("Logged in successfully. Welcome, " + activeUser.name + "!"); // Alerting user that account has been changed.
updateAfterTransactionText(); // Updating after transaction text.
break;
}
}
if (!isUserFound) alert("Account not found."); // Alerting user that account is not found.
}
function updateAccount(user) {
// Updating account information.
activeUserEl.innerHTML = `Account Name: ${user.name}`; // Updating account name.
activeUserBalanceEl.innerHTML = `Balance: ${user.balance}$`; // Updating account balance.
}
function deposit() {
// Deposit function.
if (activeUser == null) {
// Checking if user has been logged in.
alert("You must log in first.");
return;
}
if (balanceInputEl.value === "") {
// Checking if amount is empty.
alert("Please enter amount.");
return;
}
let amount = parseInt(balanceInputEl.value); // Assigning amount variable to balanceInputEl value.
if (amount < 0) {
// Checking if amount is less than 0.
alert("Value can not be less than 0$"); // Alerting user that amount is less than 0.
return;
}
activeUser.balance += amount; // Adding amount to activeUser balance.
alert(amount + "$ has been added to your account successfully."); // Alerting user that amount has been added.
updateAccount(activeUser); // Updating account information.
clearInputValue(); // Clearing balanceInputEl value.
}
function withdraw() {
// Withdraw function.
if (activeUser == null) {
// Checking if user has been logged in.
alert("You must log in first.");
return;
}
if (balanceInputEl.value === "") {
// Checking if amount is empty.
alert("Please enter amount.");
return;
}
let amount = parseInt(balanceInputEl.value); // Assigning amount variable to balanceInputEl value.
if (amount < 0) {
// Checking if amount is less than 0.
alert("Value can not be less than 0$"); // Alerting user that amount is less than 0.
return;
} else if (amount > activeUser.balance) {
// Checking if amount is greater than account balance.
alert("Insufficient balance!"); // Alerting user that amount is greater than account balance.
return;
} else {
activeUser.balance -= amount; // Subtracting amount from activeUser balance.
alert(amount + "$ has been withdrawn successfully.");
updateAccount(activeUser); // Updating account information.
clearInputValue(); // Clearing balanceInputEl value.
}
}
function updateAfterTransactionText() {
// Updating after transaction text.
let amount = parseInt(balanceInputEl.value); // Assigning amount variable to balanceInputEl value.
if (balanceInputEl.value !== "") {
// Checking if amount is not a number.
depositTextEl.innerHTML = `Balance after deposit: ${
// Updating deposit text.
amount + activeUser.balance
}$`;
withdrawTextEl.innerHTML = `Balance after withdraw: ${
// Updating withdraw text.
activeUser.balance - amount
}$`;
} else {
depositTextEl.innerHTML = `Balance after deposit: ${activeUser.balance}$`; // Updating deposit text.
withdrawTextEl.innerHTML = `Balance after withdraw: ${activeUser.balance}$`; // Updating withdraw text.
}
}
function clearInputValue() {
// Clearing input value.
balanceInputEl.value = "";
}