Advertisement
FahimHoque

Lend transactions balances out to zero

Nov 13th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function check(transactions) {
  2.     var balance = 0; // start with a zero balance
  3.     var result = []; // start with an empty array
  4.     for (var i = 0; i < transactions.length; i++) {
  5.         var transaction = transactions[i]; // get the current transaction
  6.         if (transaction[0] === 'Lend') { // if it's a lend
  7.             balance += transaction[1]; // add the amount to the balance
  8.         } else { // otherwise it's a borrow
  9.             balance -= transaction[1]; // subtract the amount from the balance
  10.         }
  11.         if (balance === 0) { // if the balance is zero
  12.             result.push(transaction); // add the transaction to the result
  13.         }
  14.     }
  15.     return result;
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement