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

我的问题是,如果 goodCount 和 reject_count 为零,则此函数返回 NaN,但我希望函数为 true 或 false,所以如果 NaN 值返回 true,您能否建议如何编写条件。

提前致谢

4

2 回答 2

1

你的意思是这样的吗?

(async () => {
  const mathjs = await import("https://cdn.skypack.dev/pin/mathjs@v7.1.0-lgPTcYfuwGwbFfYrv7Km/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>

于 2020-07-29T14:57:56.140 回答
0

一直在尝试解决类似的问题,这就是我用它来解决您的问题的方法...

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

truegoodCountreject_count都为零时返回。

于 2021-03-29T20:11:09.450 回答