scrimba
Note at 1:01
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Note at 1:01
AboutCommentsNotes
Note at 1:01
Expand for more info
index.js
run
preview
console
/* Instructions:

1. Wire up the budgeting tool so it tells the user whether or
not they can afford an item based on their available funds. 💸
2. Style it as you wish 💅

*/

let budget = document.getElementById('budget')
let expense = document.getElementById('expense')
let income = document.getElementById('income')
let message = document.getElementById('message')
let advise = document.getElementById('advise')
let localStorageTransactions = JSON.parse(localStorage.getItem("budgetN"))
let budgetN = localStorage.getItem('budgetN') !== null ? localStorageTransactions : [];
let diff = 0;

function calcResult() {

try {
budgetN = (Number(budgetN) + Number(income.value)) - Number(expense.value)
budget.textContent = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
}).format(budgetN)

} catch (err) {
console.log(err.message);

}
updateLocalStorage();


}

function advisor() {
budgetN >= 0 ? message.textContent = `You have ${new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(budgetN)} left in your savings, go ahead and make the purchase` : message.textContent = `You have overshot your budget`;


}

function displayResult(e) {
e.preventDefault();
calcResult();
advisor();

};


function updateLocalStorage() {
localStorage.setItem('budgetN', JSON.stringify(budgetN))
}

function init() {
budget.textContent = new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
}).format(budgetN)
}
advise.addEventListener('click', displayResult)
init();
Console
/index.html
LIVE