Search code examples
htmljavascript-objects

How do I calculate income and expense in an expense tracker JavaScript


I'm trying to add the value of a checked radio button in an object in another function. However there is nothing being added and no error returned.

I'm building an expense tracker and adding those value (either income or expense) is important in my calculation.

Nb: when I try to console.log in the function where i capture the checked value, it works perfectly but when i try to add that value to the array object in another function nothing is added.

const TransactionType = document.getElementsByName('selector');

addbtn.addEventListener('click', function() {
  if (expense.value == '' || amount.value == '' || date.value == '') {
    alert('Please fill all the fields!!!');
    return;
  } else {
    const userExpense = {
      id: generateID(),
      type: getTransactionType(), // here is the problem nothing is added
      date: date.value,
      expense: expense.value,
      amount: amount.value
    };
    userInput.push(userExpense);
    localStorage.setItem('data', JSON.stringify(userInput));
    location.reload();
  }
});

function getTransactionType() {
  TransactionType.forEach(item => {
    if (item.checked) {

      return item.value;
    }
  });
}
<!-- radio buttons to choose the type of transaction -->
<div class="container">
  <h3>Select the type of transaction:</h3>
  <ul>
    <li>
      <input type="radio" id="type-income" name="selector" value="income">
      <label for="type-income">Income</label>
    </li>

    <li>
      <input type="radio" id="type-expense" name="selector" value="expense">
      <label for="type-expense">Expense</label>
    </li>
    <!-- <button onclick=" getTransactionType()">see value</button> -->
  </ul>
</div>

Solution

  • The code is incomplete. There is no addbtn and no handling of displaying the localStorage

    Why do you reload?

    TransactionType.forEach(item => { 
       if (item.checked) {
         return item.value;
       }
    }); 
    

    does nothing.

    The return statement is not interesting in a forEach.

    Here is a simpler method: I use delegation.

    There is no need to test that anything is checked because you use radios. It needs more code if you were using a checkbox.

    const getTransactionType = () => document.querySelectorAll('[name=selector]:checked')[0].value;
    
    document.querySelector('.container').addEventListener('click', () => {
      console.log(getTransactionType())
    })
    <!-- radio buttons to choose the type of transaction -->
    <div class="container">
      <h3>Select the type of transaction:</h3>
      <ul>
        <li>
          <input type="radio" id="type-income" name="selector" value="income">
          <label for="type-income">Income</label>
        </li>
    
        <li>
          <input type="radio" id="type-expense" name="selector" value="expense">
          <label for="type-expense">Expense</label>
        </li>
      </ul>
    </div>