JSFiddle - React, Tailwind, and code Playground

by Daedalus

HTML

<input id="ct" placeholder="current total" />
<br />
<input id="ot" placeholder="original total" />
<br />
<input id="xp" placeholder="event exp" />
<br />
<button id="calculate">Calculate</button>
<br />
<div id='debug'></div>

JavaScript

var tests = {
  0: {
    currentTotal: 850,
    originalTotal: 1000,
    experience: 150,
    expected: 1050
  },
  1: {
    currentTotal: 1000,
    originalTotal: 3000,
    experience: 300,
    expected: 1750
  },
  2: {
    currentTotal: 1500,
    originalTotal: 2000,
    experience: 300,
    expected: 2200
  },
  3: {
  	currentTotal: 1500,
    originalTotal: 2000,
    experience: 500,
    expected: 2450
  }
};

function test_xp(current_total, original_total, event_xp) {
  var plans = {
      C: {
        mod: 1,
        until: false
      },
      B: {
        mod: 1.5,
        until: original_total
      },
      A: {
        mod: 2,
        until: original_total,
        bonus: {
          mod: 0.5,
          until: ((original_total + 500) < 2000 ? (original_total + 500) : 2000)
        }
      }
    },
    plan = "A",
    bonus = plans[plan].mod,
    until = plans[plan].until;


  var new_total = current_total, overflow = 0;
  if ((typeof plans[plan].bonus !== 'undefined')) {
    //We have a secondary bonus
    bonus = bonus + plans[plan].bonus.mod;
    until = plans[plan].bonus.until;
  }
  
  if (current_total + (event_xp * bonus) > until) {
  	overflow = (current_total + (event_xp * bonus)) - until;
    overflow = overflow - (overflow * (1 / bonus));
		new_total = until + overflow;    
  } else  {
  	new_total = current_total + (event_xp * bonus);
  }

  current_total = new_total;

  return new_total;
}

$(function() {
  $("#calculate").click(function() {
    $("#debug").html("");
    if ($("#ct").val() != '' && $("#ot").val() != '' && $("#xp").val() != '') {
      tests[tests.length] = {
        currentTotal: parseInt($("#ct").val(), 10),
        originalTotal: parseInt($("#ot").val(), 10),
        experience: parseInt($("#xp").val(), 10)
      };
    }
    $.each(tests, function(i, v) {
      var ct = v.currentTotal,
        ot = v.originalTotal,
        xp = v.experience,
        expected = v.expected,
        output = test_xp(ct, ot, xp);
     ...