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,您能否建议如何编写条件。
提前致谢
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,您能否建议如何编写条件。
提前致谢
你的意思是这样的吗?
(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>
一直在尝试解决类似的问题,这就是我用它来解决您的问题的方法...
math.evaluate("r = (goodCount / (goodCount + reject_count)); isNaN(r) ? true : r > 0.99", {
goodCount: 0,
reject_count: 0,
Total_Planned_time: 10
}).entries[0];
true
当goodCount
和reject_count
都为零时返回。