Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (50 loc) · 2.28 KB
/
Copy pathscript.js
File metadata and controls
61 lines (50 loc) · 2.28 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
const amountInput = document.querySelector('#amount-input')
const categorySelect = document.querySelector('#category-select')
const description = document.querySelector('#description')
const generalTotal = document.querySelector('#general-total')
const expensesListDiv = document.querySelector('#expense-list')
const selectFilter = document.querySelector('#filter-select')
const options = Array.from(categorySelect.options)
const categoryList = []
options.forEach((option) => {
categoryList.push(option.value)
})
const expenseTracker = new ExpenseTracker(categoryList)
const expensesList = expenseTracker.getExpenses(generalTotal)
expensesListDiv.textContent = ''
expensesList.forEach((expense) => {
expense.displayExpenses(expensesListDiv)
})
expenseTracker.categoryFilter(selectFilter, expensesListDiv, generalTotal)
document.querySelector("#form").addEventListener('submit', (e) => {
e.preventDefault()
const amountInputValue = amountInput.value
const categorySelectValue = categorySelect.value
const descriptionValue = description.value
const id = crypto.randomUUID()
if (amountInputValue === '' || parseInt(amountInputValue) <= 0){
alert('Please Enter a number greater than 0 for the amount')
}else {
const expenseDate = new Date().getTime()
const newExpenseTracker = new ExpenseTracker(categoryList)
const newExpensesList = newExpenseTracker.getExpenses(generalTotal)
let existCounter = 0
newExpensesList.forEach((expense) => {
if (expense.description !== descriptionValue || expense.category !== categorySelectValue || expense.amount !== amountInputValue){
existCounter += 1
}
})
if (existCounter === newExpensesList.length) {
const newExpense = new Expense(amountInputValue, categorySelectValue, descriptionValue, id, expenseDate)
newExpense.saveExpenses(newExpensesList)
}else{
alert('This Expense already exist in the expenses list!')
}
const newExpTrack = new ExpenseTracker(categoryList)
const newExpList = newExpTrack.getExpenses(generalTotal)
expensesListDiv.textContent = ''
newExpList.forEach((expense) => {
expense.displayExpenses(expensesListDiv)
})
}
})