-3

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

IT goldman
  • 14,885
  • 2
  • 14
  • 28
Roy Sougat
  • 83
  • 7
  • 1
    in the first you need to derive a straight line equation = `(y1-y2)/(x1-x2) = (y1-y_out)/(x1-x_inp)` – cmgchess Jul 10 '22 at 09:33

1 Answers1

3

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;
  }
}
IT goldman
  • 14,885
  • 2
  • 14
  • 28