Calculate how much of a fixed discount goes between two total - javascript

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;

Related

How implement logic for increment by 100 on random bids in JS

I am testing Max Bid functionality via Cypress but I need to randomly bid the amount in a way that it will be incremented by 100.
var minval = 100; var maxval = 1000000; var bidAmount = Math.floor(Math.random() * maxval) + minval
The logic is given above is for any random amount.
When I implement this logic I get this error: "Bids should be incremented by 100, input was 589"
Please share your ideas. Thank you!
Several errors to fix
The most important is that you have done nothing to ensure the bid amounts are multiples of 100. A simple way to do this is to divide by 100, round to the nearest integer, and then multiply by 100.
Second, it would be good to set minval to a real minimum acceptable value, namely 100.
Third, if you really want the values to go between minval and maxval, you should multiply the Math.random() by (maxval-minval) so that when you add minval, it will be in the range you want.
const minval = 100;
const maxval = 1000000;
const rawBidAmount = Math.floor(Math.random() * (maxval-minval)) + minval
const roundedBidAmount = 100*Math.round(rawBidAmount/100)
console.log(roundedBidAmount)

Modelling a numeric result against a change in percentage

I have a calculation which returns a percentage
TotalDelegatedStake / circulatingSupply * 100 = Y%
And I have a different calculation that also uses the first variable
let emissions = numberinput4.value;
let stake = numberinput2.value / TotalDelegatedStake;
let penalty = numberinput3.value <= 98.5 ? '0' : numberinput3.value / 100;
let fee = numberinput5.value == 0 ? 1 : 1 - (numberinput5.value / 100);
let calc = emissions * stake * penalty * fee;
return calc;
I would like to be able to model what the result of calc would be if Y% increased or decreased in value.
Eg. it's 30% now what would the result of calc be if it was 40%.
thanks in advance

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");
}

javascript calculating mileage rates

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)

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