HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Prorated refund calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
proratedcalculatorrefund

Problem

I wrote this little piece of script for my workplace (insurance, etc.) to help our admins, as a light and simple file I can just email to my colleagues.

The purpose of it is to calculate a prorated refund in case of a policy holder cancelling it. Basically a policy's purchase value "ticks down" gradually after a certain grace period (calculated in days), and any claims paid against the policy are then deducted from the remaining amount.

I tested for a number of valid values and it works good. I also tested for invalid input and it returns Invalid Date if either of the dates can't be reconciled to a valid Date() value, or the calculations return NaN, which is fine with me but I'm open to more elegant solutions.





Prorated Refund Calculator

body {
font-family: Consolas, Monaco, monospace;
font-size: 1.0em;
}
h1 {
font-size: 1.2em;
}

Prorated Refund Calculator

Input into the following fields and press Calculate.



  • Purchase Date:



  • Purchase Price:



  • Term (in years):



  • Cancel Date:



  • Amount paid in claims:



  • Grace period (in days):





Calculate

function CalculateProratedRefund() {
"use strict";
let input= document.getElementsByName("input");

// get initial values from HTMLCollection
let purchaseDate = new Date(input[0].value);
let purchasePrice = input[1].value;
let termInYears = input[2].value;
let cancelDate = new Date(input[3].value);
let amtPaidInClaims = input[4].value;
let gracePeriodInDays = input[5].value;

// calculations
let daysElapsed = Math.floor(( Date.parse(cancelDate) - Date.parse(purchaseDate) ) / 86400000);
let totalDays = termInYears * 365;
let percentUsed;
if (daysElapsed

Solution

Bug in the calculation

This part looks bugged:

let percentLeft = 1.0 - percentUsed;
let refundPercent = purchasePrice * percentLeft;
let proratedRefund = (purchasePrice * refundPercent) / 100;


I believe it should be:

let percentLeft = 1.0 - percentUsed;
let proratedRefund = purchasePrice * percentLeft;


Because, multiplying percentLeft with price and dividing by 100 makes no sense, effectively: purchasePrice percentLeft purchasePrice / 100. What does it mean to have purchase price squared? Why divide something by 100 that's already a rate.

I saw in your comments to other users that you used 100 as the example price. In which case you get the correct value by accident, and I guess that's the reason you didn't notice the problem.

Terminology

Some terms are misused, which makes the code (and your intentions) a bit confusing, and may also lead to bugs.

The word percent usually means a value between 0 and 100. But you're using these variables really in the sense of rates, as they are values between 0 and 1.

Magic number 86400000

Are you sure there are enough zeros in there? Of you copy pasted (as opposed to copy-typed by hand), then you're probably sure. But your readers cannot know that. If you write as 606024*1000 are you sure there are no typing errors? I think so.

Parsing dates

It seems you're parsing dates twice. Once with new Date and then again with Date.parse. Probably once would be enough.

A related usability issue, the correct date format to enter might not be obvious to all readers. It would be nice to add a placeholder text in the input box as a hint.

Code Snippets

let percentLeft = 1.0 - percentUsed;
let refundPercent = purchasePrice * percentLeft;
let proratedRefund = (purchasePrice * refundPercent) / 100;
let percentLeft = 1.0 - percentUsed;
let proratedRefund = purchasePrice * percentLeft;

Context

StackExchange Code Review Q#91743, answer score: 7

Revisions (0)

No revisions yet.