javascript

Difference in Gift Values - Free Will - Nonprofit Gifts - Full Stack - 11/15/21

// Input:
//
// estateSize - the total value of a person's estate (similar to 'net worth')
// oldGifts - list of existing gifts a person is donating to charity
// newGifts - list of new/updated gifts a person is donating to charity
//
// {
//    nonProfit: string,
//    amount: number,
//    amountType: 'dollar' | 'percent'
// }
//
// Instructions:
//
// 1. Calculate the difference in total value between the new and old gifts in dollars
// New and old gift difference example:
// Old gift: 150
// New gift: 100
// Expected output: -50
//
// Part 1 expected output: -19500
//
// 2. Return the difference in total value between the new and old gifts by non-profit
//
// Expected output:
// {
//     'Planned Parenthood': 1000,
//     'Doctors Without Borders': -500,
//     'United Way': -50000,
//     'Red Cross': 30000
// }

const estateSize = 1000000;

const oldGifts = [
  {
    nonProfit: "Planned Parenthood",
    amount: 10,
    amountType: "percent",
  },
  {
    nonProfit: "Doctors Without Borders",
    amount: 1000,
    amountType: "dollar",
  },
  {
    nonProfit: "United Way",
    amount: 5,
    amountType: "percent",
  }];

const newGifts = [
  {
    nonProfit: "Planned Parenthood",
    amount: 10,
    amountType: "percent",
  },
  {
    nonProfit: "Planned Parenthood",
    amount: 1000,
    amountType: "dollar",
  },
  {
    nonProfit: "Doctors Without Borders",
    amount: 500,
    amountType: "dollar",
  },
  {
    nonProfit: "Red Cross",
    amount: 3,
    amountType: "percent",
  }];

function giftTotal(estateSize, gifts) {
  let giftTotal = 0;
  for (let i = 0; i < gifts.length; i++) {
    if (gifts[i].amountType == 'dollar') {
      giftTotal += gifts[i].amount;
    } else {
      giftTotal += (gifts[i].amount / 100) * estateSize;
    }
  }

  return giftTotal;
}

function calculateTotalDiff(estateSize, oldGifts, newGifts) {
  //todo
  let oldGiftTotal = giftTotal(estateSize, oldGifts);
  let newGiftTotal = giftTotal(estateSize, newGifts);

  return newGiftTotal - oldGiftTotal;
}

function total(estateSize, gift) {
  if (gift.amountType == 'dollar') {
    return gift.amount;
  } else {
    return (gift.amount / 100) * estateSize;
  }
}

function calculateDiffByNonProfit(estateSize, oldGifts, newGifts) {
  //todo
  let nonProfitToDiff = newGifts.reduce((nonProfitToDiff, newGift) => {
    let nonProfitCurrentTotal = nonProfitToDiff[newGift.nonProfit];
    if (nonProfitToDiff[newGift.nonProfit] != null) {
      nonProfitToDiff[newGift.nonProfit] += total(estateSize, newGift);
    } else {
      nonProfitToDiff[newGift.nonProfit] = total(estateSize, newGift);
    }

    return nonProfitToDiff;
  }, {});

  // return oldGifts.reduce((nonProfitToDiff, oldGift) => {
  //   let nonProfitCurrentTotal = nonProfitToDiff[oldGift.nonProfit];

  //   if (nonProfitToDiff[oldGift.nonProfit] != null) {
  //     nonProfitToDiff[oldGift.nonProfit] -= total(estateSize, oldGift);
  //   } else {
  //     nonProfitToDiff[oldGift.nonProfit] = -total(estateSize, oldGift);
  //   }
  // }, nonProfitToDiff);

  oldGifts.forEach((oldGift) => {
    let nonProfitCurrentTotal = nonProfitToDiff[oldGift.nonProfit];

    if (nonProfitToDiff[oldGift.nonProfit] != null) {
      nonProfitToDiff[oldGift.nonProfit] -= total(estateSize, oldGift);
    } else {
      nonProfitToDiff[oldGift.nonProfit] = -total(estateSize, oldGift);
    }
  })

  return nonProfitToDiff;
}

calculateTotalDiff(estateSize, oldGifts, newGifts);
calculateDiffByNonProfit(estateSize, oldGifts, newGifts);
Was this helpful?