DEV Community

Googan Business Services
Googan Business Services

Posted on

Building an Arabic NPV/IRR calculator in vanilla JavaScript (bisection IRR, payback, sensitivity)

Feasibility studies in Saudi Arabia are usually read in Arabic, but most free NPV/IRR calculators online are English-only spreadsheets. We built a small Arabic, right-to-left calculator for our clients and published it as an open-source, single-file page. Here is how the core finance logic works, in about 15 lines of vanilla JavaScript.

Live tool: https://googanksa1.github.io/feasibility-calculator/
Source (MIT): https://github.com/googanksa1/feasibility-calculator

NPV

Cash flows are entered per year; the initial investment is a separate field and is treated as year 0:

function npv(r, inv, cfs) {
  return cfs.reduce((s, c, i) => s + c / Math.pow(1 + r, i + 1), -inv);
}
Enter fullscreen mode Exit fullscreen mode

IRR with bisection

Newton's method needs a derivative and can diverge on unusual cash-flow patterns. Bisection is slower but predictable: search between -99% and 1000%, and if NPV has the same sign at both ends there is no IRR in that range, so we return NaN and show "لا يوجد" (none) instead of a misleading number.

function irr(inv, cfs) {
  let lo = -0.99, hi = 10, flo = npv(lo, inv, cfs), fhi = npv(hi, inv, cfs);
  if (flo * fhi > 0) return NaN;
  for (let k = 0; k < 200; k++) {
    const m = (lo + hi) / 2, fm = npv(m, inv, cfs);
    if (Math.abs(fm) < 1e-7) return m;
    if (flo * fm < 0) { hi = m; fhi = fm; } else { lo = m; flo = fm; }
  }
  return (lo + hi) / 2;
}
Enter fullscreen mode Exit fullscreen mode

Simple and discounted payback in one function

Passing a rate of 0 gives simple payback; passing the discount rate gives discounted payback. The fractional year is interpolated inside the year where the cumulative cash flow turns positive.

function payback(inv, cfs, r) {
  let cum = -inv;
  for (let i = 0; i < cfs.length; i++) {
    const c = r ? cfs[i] / Math.pow(1 + r, i + 1) : cfs[i];
    if (cum + c >= 0) return i + (-cum) / c;
    cum += c;
  }
  return NaN;
}
Enter fullscreen mode Exit fullscreen mode

Parsing input typed on an Arabic keyboard

Arabic keyboards produce the Arabic comma (U+060C) instead of ",". A one-line split that accepts both, plus spaces, avoids a whole class of "why is my result NaN?" support questions:

value.split(/[,،\s]+/).map(s => s.trim()).filter(Boolean).map(Number).filter(n => !isNaN(n));
Enter fullscreen mode Exit fullscreen mode

Sensitivity table

Lenders and investors usually ask "what if sales drop?". The page recomputes NPV and IRR with all cash flows scaled by -30%, -20%, -10%, 0, +10% and +20%, which is often more useful than the headline numbers.

Why a single HTML file

No build step, no dependencies, works offline once loaded, and it is easy for anyone to audit. For a finance tool that people use to make real decisions, being able to read the formulas matters.


The calculator is maintained by Googan Business Services in Riyadh, which prepares feasibility studies for the Saudi market: https://googanconsult.com . Issues and pull requests are welcome on GitHub.

Top comments (0)