0
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

Ritu
  • 714
  • 6
  • 14
  • Hi there. If you found my answer helpful, please accept it. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) for more information. – Julia Mar 08 '21 at 11:37

2 Answers2

1

Do you mean something like this?

(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>
Julia
  • 1,950
  • 1
  • 9
  • 22
  • "(goodCount/(goodCount+reject_count))>0.99" con you please write condition in this string so mathjs.evaluate give me return value true – lakshit singh Jul 29 '20 at 15:02
  • 1
    I'm actually not familiar with MathJS. Let me check the documentation. – Julia Jul 29 '20 at 15:04
  • 1
    Oh, and why are you using an external library instead of just writing it as a JavaScript expression (i.e. `goodCount / (goodCount + reject_count) > 0.99`)? – Julia Jul 29 '20 at 15:10
  • @lakshitsingh you can output the function that Julia has written to the console.log with `console.log(evaluateExpression());` trying to write it into your string might be more difficult. – user115014 Jul 29 '20 at 15:11
  • I also can't reproduce your issue. When I try the code you included in your original post, [it returns `false`](https://jsfiddle.net/gtjzf6cn/). – Julia Jul 29 '20 at 15:12
  • @MikeW ..... I have issued my condition store in the database in a string form so if mathjs.evaluate give true or false according to match condition my further value comes. but if Nan value come I am got getting a response so I want Condition in string – lakshit singh Jul 29 '20 at 15:21
  • @Julia "(goodCount/(goodCount+reject_count))<0.99" in both case it return false so none of my condition satified so i want if NaN come it will true above condition – lakshit singh Jul 29 '20 at 15:29
  • I don't see where you are getting a `NaN` value? – Julia Jul 29 '20 at 15:33
  • @Julia mathjs.evaluate("(goodCount/(goodCount+reject_count))", { goodCount: 0, reject_count: 0, Total_Planned_time: 10, }); try this 0/0 give me NaN – lakshit singh Jul 29 '20 at 15:34
  • Agreed it's a little odd as I get false too when running on their site - even running `(0/0)>0.99` in the console gives a false even when `(0/0)` gives NaN... strange, could it be something to do with the environment you're running the evaluation in? – user115014 Jul 29 '20 at 15:37
0

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.

ty_rex
  • 89
  • 5