JSFiddle - React, Tailwind, and code Playground

by Scott Kaye

JavaScript

const CARD_FEE = 5;
const MIN_PAYMENTS_NO_FEE = 3;

function solution(A, D) {
	let total = 0;
	let paymentCountByMonth = new Array(12).fill(0);

	for (let i = 0; i < A.length; ++i) {
    let amount = A[i];
		let dateParts = D[i].split("-");
		let date = new Date(+dateParts[0], +dateParts[1] - 1, +dateParts[2]);

		total += amount;
		
		if (amount < 0) {
			++paymentCountByMonth[date.getMonth()];
		}
  }
	
	for (let paymentCount of paymentCountByMonth) {
		if (paymentCount < MIN_PAYMENTS_NO_FEE) {
			total -= CARD_FEE;
		}
	}
	
	return total;
}

// 230
console.log(solution([100, 100, 100, -10], ["2020-12-31", "2020-12-22", "2020-12-03", "2020-12-29"]));

//25
console.log(solution([180, -50, -25, -25], ["2020-01-01", "2020-01-01", "2020-01-01", "2020-01-31"]));
// 25