0.99", { goodCount: 0, reject_count: 0, Total_Planned_time: 10 })) my problem is tha" /> 0.99", { goodCount: 0, reject_count: 0, Total_Planned_time: 10 })) my problem is tha" /> 0.99", { goodCount: 0, reject_count: 0, Total_Planned_time: 10 })) my problem is tha"/>

how to handle NaN value in mathjs.evaluate

572 Views Asked by At
console.log(mathjs.evaluate("(goodCount/(goodCount+reject_count))>0.99", {
  goodCount: 0,
  reject_count: 0,
  Total_Planned_time: 10
}))

my problem is that if goodCount & reject_count zero this function return NaN but I want Either true or false by function so can you suggest how to write condition if NaN value came it return true.

Thanks in advance

2

There are 2 best solutions below

10
Julia On

Do you mean something like this?

(async () => {
  const mathjs = await import("https://cdn.skypack.dev/pin/[email protected]/min/mathjs.js");

  function evaluateExpression() {
    const result = mathjs.evaluate("goodCount / (goodCount + reject_count)", {
      goodCount: 0,
      reject_count: 0,
      Total_Planned_time: 10,
    });

    if (isNaN(result)) {
      return true;
    }

    return false;
  }

  document.querySelector("button").addEventListener("click", (ev) => {
    console.log(evaluateExpression());
  });
})();
<button>Evaluate the expression</button>

0
ty_rex On

Just been trying to solve a similar problem, here's how I used it to solve yours...

math.evaluate("r = (goodCount / (goodCount + reject_count)); isNaN(r) ? true : r > 0.99", {
  goodCount: 0,
  reject_count: 0,
  Total_Planned_time: 10
}).entries[0];

This returns true when goodCount and reject_count are both zero.