Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fromula to find value in y axis for given value in x axis

enter image description here

var x1 =10;
var x2 =100;
var y1 =2;
var y2 =15;
var x_inp =60;

var y_out =???

How do I calculate discount? A shopkeeper gives a discount of min 2 on purchase of 10, and max 15 on 100. What will be discount on a purchase of 1->30, 2 ->65 (formula please)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is a math problem, but computers help to solve it using linear forumla (as suggested by @cmgchess in comments):

var x1 = 10;
var x2 = 100;
var y1 = 2;
var y2 = 15;
var x_inp = 6;

// var y_out = ? ? ?
var calcY = getLinearFunction(x1, y1, x2, y2);
console.log(calcY(6))

function getLinearFunction(x1, y1, x2, y2) {
  var slope = (y2 - y1) / (x2 - x1);
  return function(x) {
    return slope * (x - x1) + y1;
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading