Reputation: 23
I am new to coding and I am struggling with this idea I want to implement.
The thing I am trying to accomplish is that you choose a amount of people on a slider from 100 to 1000 people. And beginning from 100 each person pays 10 euro and this gradually decreases(becomes cheaper) to 5 euro per person for 1000 people.
My best attempt was to do it in chunks, but this was a bad idea. Since 199 x 10 = 1990
and 200 x 9 = 1800
.
my attempt:
var people = $1;
var result = 0;
if ( people > 100 && people <= 200 )
result = people * 10;
if ( people > 200 && people <= 300)
result = result + people * 9;
return result;
So the question is how can I gradually decrease the cost per person? Thanks in advance!
I changed the code to:
var people = $1;
var result = 0;
var domain = (200 - 100) / (16.75 - 14.75);
var discount = (people - 100) / domain;
var domain2 = (300 - 200) / (14.75 - 13.75);
var discount2 = (people - 200) / domain2;
var domain3 = (400 - 300) / (13.75 - 12.90);
var discount3 = (people - 300) / domain3;
if ( people > 100 && people <=200)
result = (people * (16.75 - discount));
if ( people > 200 && people <=300)
result = (people * (14.75 - discount2));
if ( people > 300 && people <=400)
result = (people * (13.75 - discount3));
if ( people > 400)
result = (people * 12.90);
return result;
Probably not the best way to do it, or rather a stupid way, but it works. Thanks a lot everyone!
Upvotes: 2
Views: 768
Reputation: 560
It feels like this is a mathematical problem and not a coding one.
Letn = number of total people
x = number of people selected in the slider
,
maxPrice = price when x = 0
minPrice = price when x = n
You need to find an f(x)
which has these properties:
My naive approach would be using a linear function f(x) = mx + q
You can find the slope of the line m
using (minPrice - maxPrice)/n
and the q
is the value when f(0)
, which must be maxPrice
.
So you end up with this formula:
f(x) = ((minPrice - maxPrice)/n)*x + maxPrice
Which returns values in the interval [maxPrice, minPrice]
for all 0 <= x <= n
in a linear way.
Upvotes: 1
Reputation: 941
What you're looking for here is a function y=f(x)
which maps from the domain X
of positive integers between 100 and 1000 to the co-domain Y
of positive float from 5 to 10. You defined that this function must pair 1000 with 5 and 100 with 10; now you have to decide how it changes in all the between area.
That function can be linear or have any shape you prefer.
I made a desmos example with a linear function, you can have fun with any other function you like: https://www.desmos.com/calculator/8iss1umrhn
Upvotes: 0
Reputation: 7207
You need to find the right formula, writing chaining if else most of the times means that your logic is wrong.
Here is a demo of how your code might look like:
function getTotalPrice(people) {
var minPeople = 100;
var maxPeople = 1000;
var minPrice = 5;
var maxPrice = 10;
// the below calculations are based on the speculations you described in the question
var domain = (maxPeople - minPeople) / (maxPrice - minPrice);
var discount = (people - minPeople) / domain;
return '$ ' + (people * (maxPrice - discount)).toFixed(2);
}
Upvotes: 0
Reputation: 386
Some good values for your problem: coefficient: -0.00555556 intercept: 10.55555556
you can then calculate your price like this: num_people * coefficient + intercept
I assumed that the association between number of people and price is linear. If this is not the case you would need to work with a non-linear hypothesis, but I am guessing that the function described above will do the job in this case (?)
If you are interested in this stuff I can recommend some reading up on linear algebra.
some very simple example code in python for calculating these numbers with linear regression (python, because it makes the code very simple thanks to the fantastic library support):
from sklearn import linear_model
x = [[100], [1000]]
y = [[10], [5]]
reg = linear_model.LinearRegression()
reg.fit(x, y)
print(reg.coef_)
print(reg.intercept_)
There are also some websites that you can use to calculate your coefficient and intercept with linear regression. Since I don't think you will need to implement linear regression for your specific problem but just need the values.
Try https://www.hscripts.com/scripts/JavaScript/linear-regression-calculator.php for example.
Now, you will find tons of resources online if you are interested in the nitty gritty details of linear regression. I can strongly recommend the machine learning course on coursera - the first week is all about linear regression, going into a lot of detail on how the math behind the algorithm works: https://www.coursera.org/learn/machine-learning
Upvotes: 1
Reputation: 1431
Use set values for your "chunks" as shown below:
var people = <<<input value>>>;
var result = 0;
if ( people > 100 && people < 200 ) {
result = 10;
} else if ( people >= 200 && people < 300) {
result = 9.44;
} else if ( people >= 300 && people < 400) {
result = 8.88;
} else if...
return result;
Also changed your logic a wee bit (else ifs, greater thans etc.)
Upvotes: 0