Skip to content

Commit 8728f2f

Browse files
committed
69 Budgety Project
- Learnings 1. How to convert field inputs to numbers; 2. How to prevent false inputs (i.e., an empty input); - README Partially Updated: link for updated app.js has to be added.
1 parent d7d593c commit 8728f2f

2 files changed

Lines changed: 32 additions & 23 deletions

File tree

JS-Objects-Functions-Advanced/project-budgety/script/app.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
/********************************************************************************************
22
* What we'll learn
33
* ----------------
4-
* 1. How to clear HTML fields;
5-
* 2. How to use querySelectorAll();
6-
* 3. How to convert a list to an Array;
7-
* 4. A better way to loop over an array than for loop: forEach() loop;
4+
* 1. How to convert field inputs to numbers;
5+
* 2. How to prevent false inputs (i.e., an empty input);
86
*/
97

108
// Budget Controller
@@ -73,11 +71,6 @@ var budgetController = (function() { // Code related to handling the budget (
7371
};
7472
})();
7573

76-
/**
77-
* After adding new items to the UI, we also want to clear HTML fields in the UI, because it
78-
* makes sense to do that. Therefore, we do it by adding the relevant function into the
79-
* UIController's IIFE, below:
80-
*/
8174

8275
// UI Controller
8376
var UIController = (function(){ // Code to manipulate the UI
@@ -102,12 +95,15 @@ var UIController = (function(){ // Code to manipulate the UI
10295
* which are .add__type, .add_description & .add__value, referred to as
10396
* inputType, inputDescription & inputValue using the DOMStrings object
10497
* which is defined above.
105-
* type: income/expense
98+
*
99+
* type: income/expense option
100+
* description: string
101+
* value: floating point number
106102
*/
107103
return {
108104
type: document.querySelector(DOMStrings.inputType).value,
109105
description: document.querySelector(DOMStrings.inputDescription).value,
110-
value: document.querySelector(DOMStrings.inputValue).value
106+
value: parseFloat(document.querySelector(DOMStrings.inputValue).value)
111107
};
112108
},
113109

@@ -196,27 +192,39 @@ var controller = (function(budgetCtrl, UICtrl){ // Code related to handling even
196192
});
197193
};
198194

195+
var updateBudget = function() {
196+
// 1. Calculate the budget
197+
198+
// 2. Return the budget
199+
200+
// 3. Display the budget on the UI
201+
202+
};
199203

200204
var ctrlAddItem = function() {
201205
var input, newItem;
202206

203207
// 1. Get the field input data
204208
input = UICtrl.getInput(); // console.log(input); // testing
205209

206-
// 2. Add the item to the budget controller
207-
newItem = budgetCtrl.addItem(input.type, input.description, input.value);
208-
209-
// 3. Add the item to UI
210-
UICtrl.addListItem(newItem, input.type);
210+
/**
211+
* We want steps 2 and above to happen, only when the input has some description and
212+
* some income/expense value (not 0 & NaN). Therefore, we use an if statement to
213+
* handle such a scenario, right here.
214+
*/
215+
if (input.description !== "" && !isNaN(input.value) && input.value > 0) {
216+
// 2. Add the item to the budget controller
217+
newItem = budgetCtrl.addItem(input.type, input.description, input.value);
211218

212-
// 4. Clear the input HTML fields
213-
UICtrl.clearFields();
219+
// 3. Add the item to UI
220+
UICtrl.addListItem(newItem, input.type);
214221

215-
// 5. Calculate the budget
222+
// 4. Clear the input HTML fields
223+
UICtrl.clearFields();
216224

217-
// 6. Display the budget at the UI
218-
219-
// console.log("Its okay!"); // testing
225+
// 5. Calculate and update the budget
226+
updateBudget();
227+
}
220228
};
221229

222230
// to be able to call the init() function from the global scope, we return an

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,5 @@ Syntax -
7373
5. Function Constructors and Data Structures for Incomes & Expenses: [app.js](https://github.com/Ch-sriram/JavaScript/blob/d0fb4eafdf2613d8c692f8bbd4c22a63b0ead81c/JS-Objects-Functions-Advanced/project-budgety/script/app.js)
7474
6. Avoiding Data Structure Conflicts & Passing Data from One Module to Another: [app.js](https://github.com/Ch-sriram/JavaScript/blob/cc0f444858b3b1eb4860a9ee78add0b105ff9be2/JS-Objects-Functions-Advanced/project-budgety/script/app.js)
7575
7. Usage of <code>innerAdjacentHTML()</code> method & <code>replace()</code> method: [app.js](https://github.com/Ch-sriram/JavaScript/blob/ce2b3650d1745900ba8070af731c7ab75d8be2cf/JS-Objects-Functions-Advanced/project-budgety/script/app.js)
76-
8. Usage of <code>querySelectorAll()</code> method, Conversion of <code>NodeList</code> to <code>Array</code> & the usage of <code>forEach()</code> method: [app.js](https://github.com/Ch-sriram/JavaScript/blob/345d673fb41d34d6bd98de97284219196220ead5/JS-Objects-Functions-Advanced/project-budgety/script/app.js)
76+
8. Usage of <code>querySelectorAll()</code> method, Conversion of <code>NodeList</code> to <code>Array</code> & the usage of <code>forEach()</code> method: [app.js](https://github.com/Ch-sriram/JavaScript/blob/345d673fb41d34d6bd98de97284219196220ead5/JS-Objects-Functions-Advanced/project-budgety/script/app.js)
77+
9. Converting HTML Field Inputs to Numbers & Preventing False Item Inputs: [app.js]()

0 commit comments

Comments
 (0)