-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
66 lines (62 loc) · 1.87 KB
/
Copy pathindex.js
File metadata and controls
66 lines (62 loc) · 1.87 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
const expenses = document.getElementById("minus");
const income = document.getElementById("plus");
const balance = document.getElementById("money");
var transactionHistory = [];
var totalExpenses = 00;
var totalIncome = 10;
//var x = load();
expenses.innerHTML = totalExpenses + ".00$";
income.innerHTML = totalIncome + ".00$";
balance.innerHTML = totalIncome - totalExpenses + ".00$";
function add() {
var transactionBody = document.getElementById("text").value;
var transactionAmount = document.getElementById("amount").value;
if (!isFormValid(transactionBody, transactionAmount)) {
return alert("form is not valid");
}
transactionAmount = parseInt(transactionAmount);
if (transactionAmount > 0) {
totalIncome += transactionAmount;
} else {
totalExpenses -= transactionAmount;
}
transactionHistory.push([transactionBody, transactionAmount]);
updateIncome(totalIncome);
updateExpense(totalExpenses);
updateBalance(totalIncome - totalExpenses);
document.getElementById("amount").value = "";
document.getElementById("text").value = "";
document.getElementById("History").innerHTML = transactionHistory.map(
getFullName
);
}
function getFullName(item) {
var fullname = "Item " + item[0] + " price " + item[1] + ";";
return fullname;
}
function updateIncome(value) {
income.innerHTML = value + ".00$";
}
function updateExpense(value) {
expenses.innerHTML = value + ".00$";
}
function updateBalance(value) {
balance.innerHTML = value + ".00$";
}
function isEmpty(value) {
return value.trim() == "";
}
function isNotNumber(number) {
return isNaN(number);
}
function isFormValid(transactionBody, transactionAmount) {
if (isEmpty(transactionBody) || isEmpty(transactionAmount)) {
alert("please input a text");
return false;
}
if (isNotNumber(transactionAmount)) {
alert("Please input a number");
return false;
}
return true;
}