javascript calculating mileage rates - javascript

I'm creating a HR Mileage and Expenses system but am struggling to come up with a way of calculating the rates correctly.
There are 2 rates for car, motorbike, and bicycle. One rate for upto 10,000 miles one rate for over 10,000 miles.
Lets just take car rates as an example. Currently it's 45pence per mile up to 10,000 miles and 25pence per mile there after.
So I have the variables to hold the business mileage and keep it adding but how can I handle the change over of rates?
For example: BusinessMiles = 9990, Mileage Claimed = 100.
So I need to check the business miles are less than 10,000 then the difference between the business miles and the limit. which is 10 miles # 0.45 and 90 miles # 0.25.
With Chris's pointers here's my output:
//calculate mileage
var businessMilesClaimed = "100";
var currentMilesClaimed = "12110";
if (currentMilesClaimed < 10000)
{
var claimedAmount = +businessMilesClaimed + +currentMilesClaimed;
if (claimedAmount > 10000)
{
var claimCalc1 = (claimedAmount - 10000) * 0.25;
var claimCalc2 = (10000 - currentMilesClaimed) * 0.45;
var claimResult = +claimCalc1 + +claimCalc2;
}
else
{
var claimResult = businessMilesClaimed * 0.45;
}
}
else
{
var claimResult = businessMilesClaimed * 0.25;
}

This seems like something you could definitely have tackled. As such, here is some pseudocode to help you:
milage := 11,192.
// milage is the amount of miles driven..
if(milage is greater than 10000)
// If they've driven more than ten thousand miles, calculate the difference.
milage := 10000.
changeOverMilage := milage - 10000.
else
// Otherwise, there is no changeOverMilage so set it to 0.
changeOverMilage = 0.
// Calculate the cost.
cost := (milage * 0.45) + (changeOverMilage * 0.25)

Related

Calculating Minimum Lectures required for a target attendance %

The concept is simple,
Suppose I have attended 3 out of 4 lectures. My current % would be 75%
I want the % above 95. This means I need to attend 16 more classes to make it 19 out of 20 lectures.
This is what I have implemented in JS. I needed to multiply 10 at the end - not sure why the answer was coming 1/10th of the correct answer.
var present=3, absent=1;
var total = present+absent;
var CurrentPercentage = (100*present)/total;
var classReq = (95 * total)/100 - present;
classReq += (95 * classReq)/100;
console.log(classReq>0?(Math.ceil(classReq*10)):0);
It works but I think there must be a better algorithm (I am sure there must be)
Basically you have a count of lecture (20) and a percent value (95%) and visited lecture (3), you could calculate the missing count and subtract the already visited lectures.
var target = 95,
lectures = 20,
visited = 3,
needed = lectures * target / 100 - visited;
console.log(needed);
You could have a look to the missed lectures, then you could calculate the needed lectures.
var target = 95, // percent
visited = 3,
missed = 1, // this is 5%
needed = missed * 100 / (100 - target) - visited;
console.log(needed);
What about:
current=3;
max=4;
if(current/max>0.95){
alert("reached");
}else{
Alert((0.95-current/max)*100+"% needed");
}

Calculate how much of a fixed discount goes between two total

I'm having a very difficult time trying to figure out how to split a discount between two totals. Example i have a fixed discount of 100 and two totals one is 5000 the other is 468. So of that 100 like 95% is given to the 5000 and the other 5% is given to the 468. Any direction would help thank you and this is my code thus far.
subtotalDiscount = discount / 100 * subTotal;
nonTaxableTotalDiscount = discount / 100 * nonTaxableTotal;
1) 100/(5000+468) * 5000
2) 100/(5000+468) * 468
I think what you're looking for is just a way to make these values calculate automatically based on some fixed discount. Here is something you could base it on:
largeSub = 5000;
smallSub = 468;
fixedDiscount = 100;
largeDiscountPercent = .95;
smallDiscountPercent = .05;
largeTotal = largeSub - fixedDiscount * largeDiscountPercent;
smallTotal = smallSub - fixedDiscount * smallDiscountPercent;

How to distribute values linearly

I have a total of 10,000 that I want distributed among 99 points, not divided equally but on an increasing linear curve. So while the first point may be worth only [e.g.] 10, each following point would be worth more until the final one is worth [e.g.] 250 or so. But all points need total the 10,000. How could I do that?
// Edit: The first and last values of 10 and 250 are just examples, they could be anything really. The total though (10,000) needs to be variable, so I could change it to 20,000 later if needed.
Take the 99 cells with values [1,2,3,4,..,99] and multiply each number by S/4950 where S is the desired sum (e.g. S=10,000).
Starting at 3, and going up to 199, across 99 points, totaling 10,000. Is this a HW question?
var total = 0;
for (var i = 1; i < 100; i += 1) {
total += i * 2 + 1;
}
alert(total);
This is pretty vague but if the first point has value X and the gap between successive points is Y then the total of 99 such points is
(99 * X) + (0.5 * 99 * 98 * Y)
You can use this formula to play around with a suitable value of X and Y such that your total of 10,000 is satisfied. For example you could fix X first then solve the total for Y but this may not yield an integer result which may make it unsuitable. Unfortunately for certain totals there may not be integer solutions X and Y but your requirements seem rather arbitrary so I am sure you can use the above to arrive at a suitable value of X,Y and the total for your needs. It may serve you until you get a better answer.
I had a similar thing I wanted to do. Even though it is a bit late, perhaps this can help others.
Came up with the following:
var total = 10000;
var len_array = 99;
var points_array = [];
var next_no
var sum_check =0;
for (var i = 0; i < (len_array); i += 1) {
next_no = ((1- i/len_array)/(0.5 * len_array + 0.5)) * total
points_array.push(next_no);
sum_check = sum_check + next_no;
}

How to modify Elo rating to have a greater score spread?

Problem:
If you go to http://www.newedenfaces.com/ down at the bottom you can see the player leaderboard. Everyone started out with a base score 1400. There are currently over 1100 players in the database, each two being picked randomly every time you vote. As of now, the highest rating is 1572. Furthermore leaderboard is very volatile. Someone who's been in Top 10 just today is now in 70+ range.
I would like score to be more significant. Most people in the leaderboard are only a few ratings apart, and some have even rating.
Sorry for the ugly and verbose code. I'll need to refactor it later.
eloRating: function(winnerIndex) {
var kFactor = 16;
if (winnerIndex == 0) {
// A won
var ratingA = this.collection.at(0).get('rating');
var ratingB = this.collection.at(1).get('rating');
var scoreA = this.collection.at(0).get('wins');
var scoreB = this.collection.at(1).get('wins');
var expectedA = 1.0 / (1.0 + Math.pow(10, ((ratingA - ratingB) / 400)));
var expectedB = 1.0 / (1.0 + Math.pow(10, ((ratingA - ratingB) / 400)));
var newRatingA = ratingA + (kFactor * expectedA);
var newRatingB = ratingB - (kFactor * expectedA);
this.collection.at(0).set('rating', Math.round(newRatingA));
this.collection.at(1).set('rating', Math.round(newRatingB));
} else {
// B won
var ratingA = this.collection.at(0).get('rating');
var ratingB = this.collection.at(1).get('rating');
var scoreA = this.collection.at(0).get('wins');
var scoreB = this.collection.at(1).get('wins');
var expectedA = 1.0 / (1.0 + Math.pow(10, ((ratingB - ratingA) / 400)));
var expectedB = 1.0 / (1.0 + Math.pow(10, ((ratingB - ratingA) / 400)));
var newRatingA = ratingA - (kFactor * expectedA);
var newRatingB = ratingB + (kFactor * expectedA);
this.collection.at(0).set('rating', Math.round(newRatingA));
this.collection.at(1).set('rating', Math.round(newRatingB));
}
Your equation for the expected score is incorrect. For example, by your equation someone 400 points higher would have an expected score of 10/11 (0.909). This is not right, because the actual win probability is higher than this (about 0.919). Here is the real equation:
where D is the number of points in a standard deviation (normally 400 points). This equation has no closed form so a table of values which are precomputed must be used.
Also, more importantly, you are not computing the adjustment correctly. The winner gets (1-e)**k* points. The loser loses (e)**k* points where e is the expected score for the player. So, if Player A is 400 points higher than B and wins then he gets (1-0.919)*k = 1.296 points, and the loser loses 1.296 points. In your calculation the winner is getting 14.7 points (!!!) and loser is losing 14.7 points.
This is my first post but I came up with this and it appears to be a fairly concise way of doing this.
I hope it may help someone.
var aElo = 1400; // player
var bElo = 1400; // opponent
var Res = 1 // Result... 0.5 = draw, 1 = win, 0 = loss
var nElo = aElo+Math.round((32-((Math.floor(aElo/2101)+Math.floor(aElo/2401))*8)) * (Res - (1 / (1 + Math.pow(10, -(aElo - bElo) / 400)))));
alert("Players Elo was "+aElo+" but is now "+nElo);

Electricity units calculator

I'm basically using functions and if else if statements to build an electricity reading calculator.
The units given is 1236 which is a parameter of the function called elecReading. This will be used as the amount of units used and it will calculate the amount that must be paid.
However, the first 0-500 units are billed at $1 per unit. The next 500-1000 units are billed at $1.10 a unit, and over 1000 units are billed at $3.20 a unit. For example, if I used 1000 units, my bill would be $1050.
I'm unsure how I can get this working without breaking down 1236 into singular numbers manually. How can I write a calculator like this with JavaScript?
Obviously I'm not asking for the complete answer, but a push in the right direction would be very helpful at this stage!
Thanks for the help in advance
The static version would be something like:
var UNIT_PRICE_1001_OVER = 3.20;
var UNIT_PRICE_501_1000 = 1.10;
var UNIT_PRICE_UNDER_500 = 1.00;
function elecReading(units) {
var price = 0;
if (units > 1000) {
price += (units-1000) * UNIT_PRICE_1001_OVER;
units = 1000;
}
if (units > 500) {
price += (units - 500) * UNIT_PRICE_501_1000;
units = 500;
}
price += units * UNIT_PRICE_UNDER_500;
return price;
}
This is assuming the unit price ranges are 1-500, 501-1000, 1001-Inf. Obviously this can be done more generally / with less hardcoding, using a list of objects representing a price range + price per unit in said range.
Try following function. Assuming peak rates are from units 1000+, medium rates from 501-1000, and offpeak rates from 0-500.
You could change the variables names as per your requirements/understanding.
EDITED:
While loop is added to keep reducing total units until they are greater than 1000
function elecReading(units){
var totalUnits=units;
var offPeakRate=1;
var mediumRate=1.10;
var peakRate=3.20;
var totalCharges=0;
if(totalUnits>1000){
PeakUnits = totalUnits-1000;
totalCharges = totalCharges + (PeakUnits * peakRate);
totalUnits = 1000;
}
if(totalUnits > 500){
totalUnits = totalUnits-500;
totalCharges = totalCharges + (totalUnits * mediumRate);
}
totalCharges = totalCharges + (totalUnits * offPeakRate);
return totalCharges;
}
console.log(elecReading(2000));

Categories

Resources