skip to Main Content

I am building a conversion calculator. I would like to store the references and formulas for the calculator in JSON.

I do not know how to "process the variable as math".

JSON

var JSON = {
  "conversions": [{[
 {
   "FromType": "in3",
   "ToType": "ft3",
   "Math": "/1728",
   "Description": "divide the volume value by 1728"
 }...

With the "Math" being done something like this.

D1 = document.getElementById("data1").value;
var theAnswer = eval(D1 conversions[0].Math);

Example:

D1 = 25
theAnswer = D1 /1728
theAnswer = 0.0144675

Is this possible? Is there a better way?

2

Answers


  1. Chosen as BEST ANSWER

    I had previously looked at Mathjs.. But, this is the route I went:

    Screen: enter image description here

    A Json Example:

    const JSON = {
      "conversions": [
      {
        "FromType": "atm",
        "ToType": "psi",
        "MathType": "multiply",
        "Factor": 14.696,
        "Description": "multiply the pressure value by 14.696"
      },
      {
        "FromType": "atm",
        "ToType": "mmHg",
        "MathType": "multiply",
        "Factor": 760,
        "Description": "multiply the pressure value by 760"
      }, ...
    

    Then I loop the Json to find the "FromType" and "ToType" based on what the user selects in a select drop down:

      function calcFormula(){
            
            
            convFrom = document.getElementById("ConvertA").value;
            convTo = document.getElementById("ConvertB").value;
            D1 = document.getElementById("data1").value;
            
    
    let conversions = JSON.conversions;
           
      for (var i in conversions) {
        var FromType = conversions[i].FromType;
        var ToType = conversions[i].ToType;
        var MathType = conversions[i].MathType;
        var Factor = conversions[i].Factor;
        var Description = conversions[i].Description;
          
          
     
        if (FromType === convFrom && ToType === convTo ) {
         
            if (MathType === "multiply"){
                var theAnswer = (D1 * Factor);
                
            }else{
                 var theAnswer = (D1 / Factor);
                
            }
            document.getElementById("convDescripton").innerHTML = Description;
        }
      }   
    

    It is probably not as succinct as it could be, but it is working really well so far for conversions for mass, distance, pressure, etc...


  2. looks like you are trying to achieve while using eval() can work.

    I won’t recommend it because of security reasons. A better approach would be to use a safer method to process.

    Here is my suggestion:

    use a library like

    mathjs

    It is simple use the ‘mathjs’ library in the project, here is the path

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/math.min.js"></script>
    

    Then, we can use the library to evaluate math expressions.

    const math = require('mathjs');
    
    // assume the conversion is your JSON object
    const conversion = conversions[0];
    const D1 = document.getElementById("data1").value;
    const theAnswer = math.evaluate('{$D1} ${conversion.Math}');
    
    console.log(theAnswer);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search