• Resolved Radovan

    (@radovand)


    Hi,
    Is it possible to use JS object type variable into the calculated fields, as in the example below?
    When I use this code I get this error message: Unexpected end of input

    (function () {
    
    let options = [
    
        { "field": "fieldname274", "value": 12, "name": "John" },
    
        { "field": "fieldname275", "value": 25, "name": "Peter" },
    
        { "field": "fieldname276", "value": 20, "name": "Marc" },
    
        { "field": "fieldname277", "value": 10, "name": "Marie" },
    
        { "field": "fieldname278", "value": 40, "name": "George" },
    
        { "field": "fieldname279", "value": 50, "name": "Stanley" },
    
    ];
    
    let maxValue = -1,
    
        maxOccurrence = 3,
    
        results = [],
    
        message = "";
    
    for (let i = 0; i < options.length; i++) {
    
        if (maxValue < options[i].value) {
    
            maxValue = options[i].value;
    
            //reset results
    
            results = [];
    
            //add element to results;
    
            results.push(options[i].name);
    
        }
    
        else if (maxValue == options[i].value) {
    
            //add element to results;
    
            results.push(options[i].name);
    
        }
    
    }
    
    console.log(<code>max value value is ${maxValue}</code>);
    
    if (results.length > maxOccurrence) {
    
        let message = results.join(", ");
    
        console.log(<code>You are scoring ${maxValue} for ${message}?</code>);
        return maxValue;
    
    }
    
    else {
    
        let message = results.join(", ");
    
        console.log(<code>You've got the score of ${maxValue} for ${message}</code>);
        return maxValue;
    
    }
    
    })()
    • This topic was modified 3 years, 6 months ago by Radovan.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @radovand

    Yes, you can implement the preferred equation. But I’m not sure about your code.

    In the equation you should not enclose the fields names between quotes because the plugin includes them by itself:

    let options = [
    
        { "field": fieldname274, "value": 12, "name": "John" },
        { "field": fieldname275, "value": 25, "name": "Peter" },
        { "field": fieldname276, "value": 20, "name": "Marc" },
        { "field": fieldname277, "value": 10, "name": "Marie" },
        { "field": fieldname278, "value": 40, "name": "George" },
        { "field": fieldname279, "value": 50, "name": "Stanley" },
    ];

    The plugin replaces every text in the equation with format fieldname# by its corresponding field’s value. If you want to includes references to the fields’ names and not their values, you should use the |n modifier:

    let options = [
    
        { "field": fieldname274|n, "value": 12, "name": "John" },
        { "field": fieldname275|n, "value": 25, "name": "Peter" },
        { "field": fieldname276|n, "value": 20, "name": "Marc" },
        { "field": fieldname277|n, "value": 10, "name": "Marie" },
        { "field": fieldname278|n, "value": 40, "name": "George" },
        { "field": fieldname279|n, "value": 50, "name": "Stanley" },
    ];

    Additionally, your equation includes some parser errors. The lines of code like the next one are incorrect:

    console.log(<code>max value value is ${maxValue}</code>);

    The correct would be:

    console.log('<code>max value value is $'+maxValue+'</code>');

    There are other similar lines of code you must edit.

    Best regards.

    Thread Starter Radovan

    (@radovand)

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Object type variable calculatd fields?’ is closed to new replies.