Skip to content

Commit

Permalink
fix breakeven calc to find abs min diff
Browse files Browse the repository at this point in the history
  • Loading branch information
danielabar committed Oct 20, 2024
1 parent 3d10b04 commit 24775c0
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -258,22 +258,25 @@ <h2 class="text-2xl font-semibold mb-4">About This Project</h2>
return dataPoints;
};

// Find the breakeven age (where the two lines intersect approximately)
function findBreakevenAge(dataDefault, dataDelayed, ageTakingOas) {
const findBreakevenAge = (dataDefault, dataDelayed, ageTakingOas) => {
let minDifference = Infinity;
let breakevenAge = null;

for (let age = ageTakingOas; age <= finalAge; age++) {
const benefitAtDefaultAge = dataDefault.find(item => item.x === age)?.y || 0;
const benefitAtDelayedAge = dataDelayed.find(item => item.x === age)?.y || 0;

if (Math.abs(benefitAtDelayedAge - benefitAtDefaultAge) <= closeEnoughToBreakEven) {
// Start at ageTakingOas because the second dataset only starts there
for (let age = ageTakingOas; age <= (finalAge); age++) {
const benefitAtDefault = dataDefault.find(item => item.x === age)?.y || 0;
const benefitAtDelayed = dataDelayed.find(item => item.x === age)?.y || 0;
const difference = Math.abs(benefitAtDelayed - benefitAtDefault);
if (difference < minDifference) {
minDifference = difference;
breakevenAge = age;
break;
}
}
if (breakevenAge === null) {
console.log({minDifference, breakevenAge})

if (breakevenAge === finalAge && minDifference > 1000) {
console.error('Could not find breakeven age');
return "Beyond Human Lifetime"
return "Off the chart!"
}
return breakevenAge;
};
Expand Down

0 comments on commit 24775c0

Please sign in to comment.