ChrisGuru
ChrisGuru

Reputation: 107

I am trying to convert a dynamic formula in Javascript

this is not a regular JS tasks as I need help with JS and maths little bit. I got the math formula done but now I'm trying to get it in javascript.

I writing a calculator so I got var a,b,c and D.

D is the sum of A,B,C (Currently it is 825)

I changed the D from 825 to 600 so everything should reduce propotionaly but not B. B should stay as it is and A and C should reduce.

The following code below calculate correctly but I want to make it dynamic fully. Var newA and newC I can't seem to get the right variable in it as I need the newA to dive by 6 and New C to divide by 5/6.

Is there any genius good in math and javascript? I also attached an image of the math formula if anyone can maybe write a better JS. enter image description here The idea is if that I can after add more variables and when I reduce the D it will let me know how much are the elements that can be reduced.

Here is the code:

// Variables
var a = 100; 
var b = 225; // this variable does not change.
var c = 500;

// Container sum
var containerSum = a + b + c ;

// New container sum
var newContainerSum = 600;

//  825 - (100 + 500) = 225
var fixedElement = b;

// 600 - 225 = 375
var remainingElementsTotal = newContainerSum - fixedElement;

//Propotion calculate 100/500 = 1/5 = 0.2
var propotions = a/c;
var remainingPropotions = 1 - propotions;


var newA = remainingElementsTotal/6;  
var newC = remainingElementsTotal * (5/6);

Upvotes: 0

Views: 1488

Answers (2)

cybersam
cybersam

Reputation: 66989

Here is a general function that takes the original array of values (vals), the index of the fixed value (fixedIndex), and the new sum (newSum); and returns a new array with suitably modified values:

function calc(vals, fixedIndex, newSum) {
  var initSum = vals.reduce(function (prev, cur) {return prev + cur;}, 0);
  var fixedElement = vals[fixedIndex];
  var initRemainingSum = initSum - fixedElement;
  var endRemainingSum = newSum - fixedElement;
  return vals.map(function(cur, index) {return index === fixedIndex ? cur : endRemainingSum*(cur/initRemainingSum);})
}

For example:

calc([100, 225, 500], 1, 600)

returns:

[ 62.5, 225, 312.5 ]

[EDITED]

To answer your follow-on question in the comments, if you want to specify an array of fixed indices (fixedIndices), this should work:

function calc2(vals, fixedIndices, newSum) {
  var initSum = vals.reduce(function (prev, cur) {return prev + cur;}, 0);
  var fixedSum = fixedIndices.reduce(function (prev, cur, index) {return prev + vals[cur];}, 0);
  var initRemainingSum = initSum - fixedSum;
  var endRemainingSum = newSum - fixedSum;
  return vals.map(function(cur, index) {return fixedIndices.indexOf(index) === -1 ? endRemainingSum*(cur/initRemainingSum) : cur;})
}

For example:

calc2([100, 225, 500], [1,2], 600)

returns:

[ -125, 225, 500 ]

Upvotes: 1

vkstack
vkstack

Reputation: 1644

I believe this is what you seeking for.

var elements = [100, 300, 225, 560, 900],newElements=[];
var containerSum = elements.reduce(function(prev, cur) {
    return prev + cur;
});
var newContainerSum = 1500;
var indexToBeFixed = 1;
var remainingElementsTotal = newContainerSum - elements[indexToBeFixed];
for (var i = 0; i < elements.length; i++) {
  newElements.push(i !== indexToBeFixed?(elements[i] * remainingElementsTotal) / (containerSum - elements[indexToBeFixed]):elements[i] );
}

console.log(newElements);

/*
[ 67.22689075630252,
  300,
  151.26050420168067,
  376.47058823529414,
  605.0420168067227 ]
*/

Update

elements array will be modified.

Upvotes: 0

Related Questions