-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (106 loc) · 3.82 KB
/
script.js
File metadata and controls
114 lines (106 loc) · 3.82 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
// const user = prompt("Enter your name");
// const welcomeMessage = prompt("Welcome " + user + " what tier are you willing to opt for?");
// const successMessage = alert("Congratulations " + user + " you've been successfully registered.");
const userName = document.getElementById("name");
const amount = document.getElementById("amount");
const tier = document.getElementById("tier");
const form = document.getElementById("form");
const notification = document.getElementById("notification");
const notification_text = document.getElementById("notification_text");
const tableBody = document.getElementById("tableBody");
const totalMoney = document.getElementById("total_money");
const interest = document.getElementById("interest");
const withdraw = document.getElementById("withdraw");
const user_details = [];
form.addEventListener("submit", (e) => {
e.preventDefault();
const user_name = userName.value;
const input_amount = parseInt(amount.value);
const input_tier = parseInt(tier.value);
const checker = validation(user_name, input_amount, input_tier);
if (checker) {
let interestValue = 0;
if (input_tier === 1) {
interestValue = (7 / 100) * input_amount;
}
if (input_tier === 2) {
interestValue = (12 / 100) * input_amount;
}
if (input_tier === 3) {
interestValue = (25 / 100) * input_amount;
}
let widthdrawValue = interestValue + input_amount;
NotificationHandler(
{
text: "You've Successfully Registered",
interest: `Your interest N${parseInt(
interestValue.toFixed(0)
).toLocaleString()}`,
withdraw: `Total to be withdrawn at the end of the week N${parseInt(
widthdrawValue.toFixed(0)
).toLocaleString()}`,
},
"success"
);
const user_detail = {
username: user_name,
amount: input_amount,
tier: input_tier,
};
user_details.push(user_detail);
tableBody.innerHTML = null;
populateTable();
}
});
const NotificationHandler = (message, status) => {
notification_text.innerHTML = message.text;
interest.innerHTML = message.interest;
withdraw.innerHTML = message.withdraw;
notification.style.backgroundColor = status === "error" ? "red" : "green";
};
const ValidateTier = (amount, tier) => {
if (tier === 1 && amount !== 10000) {
return { status: false, message: "Value for Tier is 10,000 Naira" };
} else if (tier === 2 && amount !== 20000) {
return { status: false, message: "Value for Tier is 20,000 Naira" };
} else if (tier === 3 && amount !== 30000) {
return { status: false, message: "Value for Tier is 30,000 Naira" };
} else {
return { status: true, message: "Success" };
}
};
const validation = (username, amount, tier) => {
const tierValidate = ValidateTier(amount, tier);
if (username.trim() !== "" && tierValidate.status) {
return true;
} else {
if (!tierValidate.status) {
NotificationHandler(
{ text: tierValidate.message, interest: "", withdraw: "" },
"error"
);
return false;
} else [NotificationHandler("User name cannot be empty", "error")];
}
};
const populateTable = () => {
let total_money = 0;
user_details.forEach((user, index) => {
const tr = document.createElement("tr");
const sn = document.createElement("td");
const name = document.createElement("td");
const tier = document.createElement("td");
const amount = document.createElement("td");
total_money = total_money + user.amount;
sn.innerHTML = `${index + 1}.`;
name.innerHTML = user.username;
tier.innerHTML = `Tier ${user.tier}`;
amount.innerHTML = `N${user.amount.toLocaleString()}`;
tr.append(sn);
tr.append(name);
tr.append(tier);
tr.append(amount);
tableBody.append(tr);
});
totalMoney.innerHTML = `Total amount of all riders N${total_money.toLocaleString()}`;
};