Skip to content
Advertisement

Fromula to find value in y axis for given value in x axis [closed]

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)?

Advertisement

Answer

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;
  }
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement