Apply your knowledge to build something amazing!
:information_source: Project Overview Difficulty Level: Intermediate
Estimated Time: 1-2 hours
Skills Practiced: DOM Manipulation, Event Handling, Basic Calculations, Input Validation
What you'll do: Open the project and familiarize yourself with the interface
DO NOT DELETE the existing files in the template:
ONLY EDIT the necessary files.
Let's create a budget planner! We'll be able to calculate how much money we can save and how much we can spend every week.
This is the flow of the budget planner:
graph LR
A[Phase 1: Setup] --> B[Phase 2: DOM Selection]
B --> C[Phase 3: Create Logic]
C --> D[Phase 4: Event Handling]
D --> E[Phase 5: Display Results]
E --> F[Phase 6: Testing]
style A fill:#e1f5e1
style B fill:#e1f5e1
style C fill:#fff3cd
style D fill:#fff3cd
style E fill:#d1ecf1
style F fill:#d1ecf1
We want to be able to calculate our spendings and show all the values in the cards:
Here is an example of RM50 for income and RM10 for savings. After calculation, we will get RM40 for spendings.
:bulb: Tip [Before You Begin] Take a moment to study the HTML structure in the template. Understanding how the elements are organized will make your JavaScript implementation much smoother!
Study the HTML and see if you can understand it before you start working on the code. Look for:
Let's start by declaring the necessary variables and select using DOM:
#income
#savings
#calculateBtn
#showIncome
#showSavings
#showSpendings
Like this:
var income = document.querySelector("#income");
:warning: Common Mistake Make sure you're using JavaScript syntax, not Python! Notice the
var
keyword and the semicolon at the end.
Before moving on, ensure you have:
Create a function called spendings()
and return the spendings value.
return
keyword inside the function.Use .value
to get the value of the inputs:
income.value
savings.value
:bulb: Tip [Think About It] What's the mathematical formula for calculating spendings? If you have RM50 income and want to save RM10, how much can you spend?
Add a click event listener to the calculate button.
When user clicks on the calculate button, it will set the proper money values using .textContent
for
showIncome
showSavings
showSpendings
Like this:
showIncome.textContent = income.value;
You can call the spendings()
function to get the value for spendings.
.toFixed()
method to set the value to 2 decimal places.:warning: Type Conversion Alert
.value
is a string. Use the + symbol to convert to number:(+income.value)
.toFixed()
method only works with numbers.
:bulb: Tip [Best Practice] Always format currency values consistently. Using
.toFixed(2)
ensures your money values always show two decimal places, making your app look professional!
Before testing, ensure you have:
spendings()
function with the correct formula.toFixed(2)
formattingIf you have done it correctly, it should look something like this:
If your budget planner isn't working correctly, check these common issues:
Nothing happens when clicking the button?
#calculateBtn
)Calculations show NaN?
Display shows wrong format?
.toFixed(2)
is applied correctly.textContent
, not .innerHTML
:bulb: Tip[Pro Debugging Tip] Use
console.log()
to check your values at each step. For example:
console.log("Income value:", income.value);
console.log("Savings value:", savings.value);
console.log("Calculated spendings:", spendings());
Great job completing the basic budget planner! Ready to level up? Try these challenges:
Add validation so that it will not show the spendings if the input savings is higher than income. Display "ERROR" instead of the RM value.
Add more description to the Savings card to show:
Create another HTML section below the cards to show a list of things you will spend with the Spending Money. Include:
:bulb: Tip[Encouragement] Remember, the Extension Challenges are optional but highly recommended! Each challenge helps you practice different skills:
Start with Challenge 1 and work your way up. You've got this! :muscle:
Before submitting, ensure your budget planner:
When you have completed your "Budget Planner" project, submit it using the link below:
Make sure to test your webpage before submitting to ensure all required elements are working properly!
:warning: Submission Reminder Test your budget planner with different values before submitting:
- Try normal values (income: 100, savings: 20)
- Try edge cases (income: 0, savings: 0)
- Try decimal values (income: 55.50, savings: 10.25)