Javascript function that calculates based on increments - javascript

I need assistance creating a javascript function that performs the below logic.
If cubic ft is between 2500-5000 invoice $42, but if greater than 5000 then invoice $42 plus $22 per 1000 cubic ft.
Here is what I have so far:
var assessfee = function(cubicft) {
if (cubicft > 2500 || cubicft <= 5000) {
console.log($42);
} else if (cubicft > 5000) {
var diff = cubicft - 5000;
}
}

Something like this? Basically that sets the price to 42 since that is the default price and if cubicft is > 5000 it takes that divided by 1000 then takes it times 22 and adds that value to price. Finally it prints it to the console.
var assessfee = function(cubicft) {
var price = 42;
if (cubicft > 5000) {
price += (cubicft-5000)/1000*22;
}
console.log("$" + price);
}

Related

Develop a decreasing price function by step

I'm trying to configure my price list. The price unit must decrease depending the quantity you buy but should keep growing.
I was able to do it in excel, but now I would like to do a function in javascript.
my pricelist in the DB is like this:
min max price
1 9 50
10 19 45
20 29 40
30 49 35
50 79 30
80 90 25
The function depending the price is
qty price function
1-9 qty*50
10-19 (qty-9)*45+(9*50)
20-29 (qty-19)*40+(19*45)
...
80-90 (qty-79)*25+(79*30)
I would like to create a ES6 function that receive the quantity in parameter and return the total price.
UPDATE
I was able to create the function but I'm pretty sure that we can improve it.
let priceList= [
{"_id":1,"min":1,"max":9,"price":50},
{"_id":2,"min":10,"max":19,"price":45},
{"_id":3,"min":20,"max":29,"price":40},
{"_id":4,"min":30,"max":49,"price":35},
{"_id":5,"min":50,"max":79,"price":30},
{"_id":6,"min":80,"max":90,"price":25}
]
function sum(qty){
let sum = 0;
let prices = priceList.filter((x)=>x.min<=qty);
var price;
var priceArr=[];
const pricesLength = prices.length;
for (let i = 0; i < pricesLength ; i++) {
price = prices[i];
if(i==0){
priceArr.push(price.price * ((price.max < qty) ? price.max : qty));
} else {
priceArr.push(price.price * ((price.max <= qty) ? price.max - price.min + 1 : qty - price.min + 1) + priceArr[i-1]);
}
}
return priceArr[pricesLength-1];
}
https://jsfiddle.net/jm7teuqb/1/

Waterpay calculator else if statements

I've been going over this question now for a couple of days and I'm still no closer to getting it right or understanding as to how to get it to run properly.
This is the current code I have:
let waterPay = prompt("Please enter the amount of water you use to get a price you need to pay, thank you!");
if (waterPay < 6000) {
console.log("The number is below 6000");
console.log (waterPay / 1000); //The outcome of this must be saved as a different let
console.log (waterPay * 15.73);// outcome of the above times by this amount
}
else if (waterPay > 6000 && waterPay <= 10500) {
console.log("The number is between 6000 and 10500");
}
else if (waterPay > 10500 && waterPay <= 35000) {
console.log("The number is between 10500 and 35000");
}
else if (waterPay > 35000) {
console.log("The number is above 35000");
}
What my code needs to do is take an input from the user stating how many litres of water they use, you can see in the code that depending on the amount of litres they use it should print out how much they owe.
The table above states that the first 6 000 litres will cost R15.73 per kilolitre.
Next, water consumption above 6 000 litres but below 10 500 litres will be
charged at R22.38 per kilolitre. Therefore, a household that has used 8000
litres will pay R139.14 (15.73 x 6 + 22.38 x 2). The table carries on in this
manner.
Im battling to figure out how I should go about working this out. Any help would be appreciated.
The data structure needed is something that pairs rates with usage thresholds. The last threshold is effectively infinite, to catch any usage above the highest. The logic is to find() the right rate object and multiply that rate tier's rate by the usage.
let rateData = [{
upTo: 6000,
rate: 15.73
},
{
upTo: 10500,
rate: 22.38
},
{
upTo: 35000,
rate: 34.0. // made this one up, not in the OP
},
{
upTo: Number.MAX_SAFE_INTEGER,
rate: 50.0. // made this one up, not in the OP
}
];
function rateDatumForUsage(usage) {
return rateData.find(r => usage <= r.upTo);
}
function costForUsage(usage) {
const rateDatum = rateDatumForUsage(usage);
return usage * rateDatum.rate;
}
console.log(`The cost of using 5000 units is (15.73*5000) ${costForUsage(5000)}`)
console.log(`The cost of using 10000 units is (22.38*10000) ${costForUsage(10000)}`)
console.log(`The cost of using 100000 units is (50*100000) ${costForUsage(100000)}`)
Total cost should be calculated by steps.
This means that, for example, if the first 10 liters cost USD 2, the following 10 liters (from 10 to 20) cost USD 1 and from 20 cost will be USD 0.5, then the total cost for 30 liters will be: 10*2 + 10*1 + 10*0.5 = 35.
This can only be achieved generically by looping. Here is the code:
const steps = [
6000,
10500,
35000
];
const rates = [
10,
20,
30
];
function calculate(used) {
let output = 0;
for (let i = 0; i < steps.length; i++) {
if (used >= steps[i]) {
output += steps[i] * rates[i];
} else {
output += (used - (steps[i - 1] || 0)) * rates[i];
break;
}
}
return output;
}
console.log(calculate(3000));
console.log(calculate(6000));
console.log(calculate(9000));
console.log(calculate(50000));

Javascript decimal increment?

I've got the following variable JS:
http://jsfiddle.net/c8u8wLsL/13/
$(document).ready(function () {
var total = 15.5,
value = 0,
elem = $('div');
var interval = setInterval(function () {
elem.text(value.toFixed(1) + '$');
if (value >= total) {
clearInterval(interval);
}
value = value + 0.1;
}, 5);
});
Two questions:
The resulting number is 15.6 why?
How can I make the incrementation spend the same amount of time from 0 to the target value? (from 0 to 25 spends the same time as from 0 to 250)
You forget to exit from function. Also you should probably update node's text after checking total.
if (value >= total) {
return clearInterval(interval);
}
elem.text(value.toFixed(1) + '$');
fiddle http://jsfiddle.net/Lqxsh39q/
To solve second problem you can pre-calculate duration of each interval before it setup. And use it like second argument in setInterval. Something like duration = 1000 / (total * 10); or any formula that you want.
fiddle: http://jsfiddle.net/Lqxsh39q/1/
#Glen Swift's answer is correct but I have to point out regarding your original code:
You get the resulting number as 15.6 because:
When you think you are getting 15.5 as the result, you are actually getting 15.4999999, which is smaller than 15.5 and hence the if condition is false even if you think it is true. So it gets incremented once again, giving the final result as 15.6.
As far as the second part is concerned, to get the same time, you need to have the same number of steps for each addition rather than the fixed 0.1. Let's say you want to reach the target in 100 steps everytime, you can divide the total interval by 100 and then replace it in the code where you are writing 0.1 currently.
The final code should look something like:
$(document).ready(function () {
var total = 15.5,
value = 0,
ment=(total-value)/100,
elem = $('div');
var interval = setInterval(function () {
if (value >= total) {
return clearInterval(interval);
}
elem.text(value.toFixed(1) + '$');
value = value + ment;
}, 5);
});
See the fiddle here
Your existing code:
var interval = setInterval(function () {
elem.text(value.toFixed(1) + '$');
if (value >= total) {
clearInterval(interval);
}
value = value + 0.1;
}, 5);
It should check for the >= total condition first. if condition fails then exit the function.
But you are modifying the text element before the check. thus your error.
This would do.
var interval = setInterval(function () {
value = value + 0.1;
if (value <= total) {
elem.text(value.toFixed(1) + '$');
} else {
clearInterval(interval);
}
}, 5);

Iterate through result until 0 javascript

I have written a javascript function which accepts a number of variables to produce a result what I need to do is produce a result which is 0.00 (+/- 0.01) by adjusting a percentage value that is passed to the function.
Fiddle: http://jsfiddle.net/jerswell/33vyvm6n/
If you select the first item from the list you will see the table updates with results and from there a user can enter a value into the Price ($) field say 100 click calculate and the results panel will show the results of the calculation.
The YTM when selected is 4.371 which produces a result of a Price ($) = 8.52
What I need to achieve is to show a result of 0.00 (+/- 0.01) by iterating through the YTM value and decrementing or incrementing by 0.001 until this result is achieved, for this example a YTM of 6.002 gets us close enough as we are happy with a +/- 0.01 variation in the output.
On line 114 of the fiddle there is an if statement that I have started but I am now stuck as where to go from here.
if (bondCalculation.calculatedPrice !== 0) {
}
Binary search will work. The idea is to start with a low YTM value of 0 and a high value of, say, 12000. Then you take the average of the low and high values, look at the error, and adjust the low or high end accordingly. Keep doing this until the error is sufficiently small.
You can replace
if(bondCalculation.calculatedPrice !== 0) {
}
with
function getPrice(ytm) {
return bondCalc(bond_term, bond_coupons, bond_semi_function, ytm, bondFaceValue, xtbPrice).calculatedPrice;
}
var low = 0, high = 12000, ytm;
var count = 0;
while (true) {
count += 1;
if (count == 100) {
break;
}
ytm = (low+high)/2;
if (Math.abs(getPrice(ytm)) < 0.0001) {
break;
} else if (getPrice(ytm) > 0) {
low = ytm;
} else {
high = ytm;
}
}
ytm = Math.round(1000*ytm)/1000;
yieldToMaturity.val(ytm);
bond_indicative_yield = ytm;
bondCalculation = bondCalc(bond_term, bond_coupons, bond_semi_function, bond_indicative_yield, bondFaceValue, xtbPrice);
to obtain this fiddle: http://jsfiddle.net/yow44mzm/
Try something like this, adjust variables/params as required:
if(calculatedPrice !== 0){
var currentPrice = calculatedPrice;
var adjustedYTM = ytm + 0.01;
calculatedPrice = calculatePrice(ytm, other, params);
if(calculatedPrice > currentPrice)
adjustedYTM = decrementYTM(ytm);
else
adjustedYTM = incrementYTM(ytm);
ytm = adjustedYTM;
}
function incrementYTM(ytm){
while(calculatedPrice > 0){
ytm += 0.01;
calculatedPrice = calculatePrice(ytm, other, params);
}
return ytm;
}
function decrementYTM(ytm){
while(calculatedPrice > 0){
ytm -= 0.01;
calculatedPrice = calculatePrice(ytm, other, params);
}
return ytm;
}

Incrementing a number smoothly with a variable time period in JS

I have a really simple JS counter which I display on a dashboard like screen which does the following:
Every 5 minutes it makes an jsonp call and retrieves a "total" number
It then displays this number to the screen by incrementing the last total displayed till it is equal to the new total. (the number can only ever increase)
I'm having some trouble with making the number increment smoothly. What I would like to do is find a delta (i.e. New total - old total) and increment the number gradually over the 5 minutes till the next call so it looks like a nice smooth transition.
Any ideas on how I can do this?
Currently some of my code looks like this (This block get's called every 5mins. And yes, it's in dire need of a refactor...)
var LAST_NUMBER_OF_SESSIONS = null;
var five_minutes_in_seconds = 300;
var new_number_of_sessions;
$.getJSON('http://blah.com/live_stats/default_jsonp.aspx?callback=?', function(data) {
if(LAST_NUMBER_OF_SESSIONS === null){
LAST_NUMBER_OF_SESSIONS = data.total_sessions;
}
new_number_of_sessions = data.total_sessions;
var delta = Math.floor(new_number_of_sessions - LAST_NUMBER_OF_SESSIONS);
var time_interval = (five_minutes_in_seconds / delta) * 1000;
var old_value = LAST_NUMBER_OF_SESSIONS;
var new_value = null;
sessions_interval = setInterval(function (){
new_value = parseInt(old_value, 10) + 1;
$('#stats').text(new_value);
old_value = new_value;
if(new_value >= new_number_of_sessions){
clearInterval(sessions_interval);
}
}, time_interval);
LAST_NUMBER_OF_SESSIONS = new_value;
});
}
This code it seems to increment the number very quickly at the start of the 5min period and then stop so it's not exactly right...
Try this:
var total = 0,
delta = 0,
stats = $('#stats').text( total );
function increment() {
var v = +stats.text();
if ( v < total ) {
stats.text( v + 1 );
} else {
$.getJSON('http://...', function(data) { // added data here
delta = Math.floor( 300000 / ( data.total_sessions - total ) );
total = data.total_sessions;
});
}
setTimeout(increment, delta);
}
Update:
In order to test my code, I had to simulate the JSON reponse - I used an array of numbers. See here: http://jsfiddle.net/simevidas/MwQKM/
(In the demo, I use an interval of 5 seconds instead of 5 minutes.)
I am not exactly sure why your code doesn't work as expected, although I suspect that it has to do with line LAST_NUMBER_OF_SESSIONS = new_value;. I wrote something similar and it works fine. It's not that different from what you have, minus that last line of code.

Categories

Resources