JSFiddle - React, Tailwind, and code Playground

by Gregory Nikitas

HTML

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<div id="aa_div">

</div>
<div id="ab_div">

</div>
<div id="ac_div">

</div>

JavaScript

var amount = {
  a:[10,10],
  b:[7.5,7.5],
  c:[2.5,2.5,2.5,2.5]
}

// Contains objects, which contain an array of other objects
// that represent some selection
// Each object in an array consists of different values per attribute per 1 amount
var ex_obj = {
  a: [
    {aa: 1.41, ab: 0.143, ac: 0.5},
    {aa: 1.30, ab: 1.43, ac: 0.42}
  ],
  b: [
    {aa: 0.32, ab: 5.54, ac: 1.3},
    {aa: 0.33, ab: 4.49, ac: 2.5}
  ],
  c: [
    {aa: 0.54, ab: 1.4, ac: 3.5},
    {aa: 0.39, ab: 1.434, ac: 3.91},
    {aa: 0.231, ab: 1.44324, ac: 2.91},
    {aa: 0.659, ab: 1.554, ac: 3.9124},
  ]
}

var current = [{aa:0,ab:0,ac:0}];

// Here we have an object that represents
// the "to be" state which should be achieved
var should_be ={
  min_aa: 14.534,
  max_aa: 17.432,
  min_ab: 3.43,
  max_ab: 4.3,
  min_ac: 5.534,
  max_ac: 7.21
}

window.calculate = function() {
  var ex_objClone = $.extend(true, {}, ex_obj);
  // Now we want to mulitply the amount object values with
  // the ex_obj values
  
    for(let prop in ex_obj)
      for(var i = 0; i < ex_obj[prop].length; i++)
        for(let propa in ex_obj[prop][i])
        {
          // here every aa,ab,ac gets mulitplied with the
          // contains of the amount obj for the matching
          // propertyname
          ex_objClone[prop][i][propa] = ((ex_objClone[prop][i][propa] * 10) * (amount[prop][i] * 10))/100;
        }
  	// the next step is to check if the sum of all ex_obj property
  	// child values per aa, ab and ac match the should_be propertie values
    sumprop(ex_objClone);
    for(let c in current[0])
    {
      // we need to check which property is too little [or too much]
      if(current[0][c] > should_be["min_"+c] && current[0][c] < should_be["max_"+c])
      {
      	//good
      }
      else
      {
        var maxage = -999999;
        var minage = 999999;
        var ende = undefined;
        var ind = -1;
          // if the sum of the ex_obj properties is too little
  				// compared to the should_be...