Advertisement
Guest User

add salary to tldr stock options

a guest
Jun 2nd, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function createSalary() {
  2.     var outcome = document.querySelector('.outcome');
  3.     var el = document.querySelector('.salary-guess');
  4.     if (el) {
  5.         outcome.removeChild(el);
  6.     }
  7.     var div = document.createElement('div');
  8.     div.setAttribute('class', 'salary-guess');
  9.     div.innerHTML = "<div class='salary-guess'><input class='irr' type='text' placeholder='IRR %' value='7'></input><div class='calculated-profit salary'></div><div class='steps'></div></div>";
  10.     outcome.appendChild(div);
  11.  
  12.     function newtonsMethod(f, df, x0) {
  13.         console.log("x : " + x0);
  14.         console.log("f : " + f(x0));
  15.         console.log("df: " + df(x0));
  16.         return x0 - f(x0) / df(x0);
  17.     }
  18.     function solveNpv(total, years, irr) {
  19.         if (total <= 0 || years <= 0) {
  20.             return 0;
  21.         }
  22.         function computeGrowth(salary){
  23.             var s = 0;
  24.             for (var y = 0; y < years; y++) {
  25.                 s = s*(1 + irr/100) + salary;
  26.             }
  27.             return s - total;
  28.         }
  29.         function dComputeGrowth_dSalary(salary) {
  30.             return (computeGrowth(salary*1.01) - computeGrowth(salary)) / (salary * 1.01 - salary);
  31.         }
  32.         var steps = 0;
  33.         var error = 11;
  34.         var guess = 100;
  35.         while (steps < 100 && Math.abs(error) > 10) {
  36.             guess = newtonsMethod(
  37.                 computeGrowth,
  38.                 dComputeGrowth_dSalary,
  39.                 guess
  40.             );
  41.             steps = steps + 1;
  42.             error = computeGrowth(guess);
  43.             console.log(error);
  44.         }
  45.         document.querySelector('.steps').innerText = steps + ' steps to solve';
  46.         document.querySelector('.salary').innerText = '$' + guess.toFixed(0) + ' /year (not adj)';
  47.     }
  48.     document.querySelector('input.exit-value').addEventListener('change', recompute);
  49.     document.querySelector('input.irr').addEventListener('change', recompute);
  50.     function recompute() {
  51.         var years = parseInt(document.querySelector('.calculated.years').innerText);
  52.         console.log('years ' + years);
  53.         var payout = parseInt(document.querySelector('.calculated.profit').innerText.replace(/\$|,/g,''));
  54.         console.log('payout ' + payout);
  55.         var irr  = parseInt(document.querySelector('input.irr').value);
  56.         solveNpv(payout, years, irr);
  57.     }
  58. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement