0

I have these two validation routines with the jQuery validate plugin. They work fine, but our JS compressor does not like the ES6 functions. I am new to ES6. The simple conversion I can handle, but its these lines I don't get:

let total = v.reduce((acc, n) => {
    return acc + n;
}, 0);

The two full functions are below, i know I can replace "let" with var", but how do I rewrite the arrow statements when the left side of the operator is another function. Thats where I am lost. Anybody know?

// ABN
    $.validator.addMethod('ABN', function(v, element, p) {
        if (this.optional(element)){return true};
            
                const weights = new Array(10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19);
                // Convert to string and remove all white space
                v = v.toString().replace(/\s/g, "");
                // Split it to number array
                v = v.split('').map(n => parseInt(n));
                // Subtract 1 from the first (left-most) digit of the ABN to give a new 11 digit number
                v[0] = v[0] - 1;
                // Multiply each of the digits in this new number by a "weighting factor" based on its position as shown in the table below
                v = v.map((n, i) => n * weights[i]);
                // Sum the resulting 11 products
                let total = v.reduce((acc, n) => {
                    return acc + n;
                }, 0);
                // Divide the sum total by 89, noting the remainder
                if(total % 89 === 0) {
                    return true;
                } else {
                    return false;
                }
                
    });

    // ACN
    $.validator.addMethod('ACN', function(v, element, p) {
        if (this.optional(element)){return true};

         const weights = new Array(8, 7, 6, 5, 4, 3, 2, 1);
            // Convert to string and remove all white space
            v = v.toString().replace(/\s/g, "");
            // Split it to number array
            v = v.split('').map(n => parseInt(n));
            // Set the check digit and remove it 
            let checkDigit = v.pop();
            // Apply weighting to digits 1 to 8.
            v = v.map((n, i) => n * weights[i]);
            // Sum the products
            let total = v.reduce((acc, n) => {
                return acc + n;
            }, 0);
            // Divide by 10 to obtain remainder
            let calculatedCheckDigit = (10 - (total % 10)) % 10;
            // calculatedCheckDigit should match check digit
            if(calculatedCheckDigit === checkDigit) {
                return true;
            } else {
                return false;
            }
        
    });
4
  • 1
    What do you mean by "when the left side of the operator is another function"? Commented Sep 27, 2022 at 23:37
  • Just use Babel, it will do this rewriting in an automated and correct way! Commented Sep 27, 2022 at 23:37
  • 2
    You really ought to update your "JS compressor" to one that can handle normal syntax, not rewrite your javascript to legacy syntax unnecessarily. Commented Sep 27, 2022 at 23:38
  • Yes... I know.. I need to... this is just a quick fix.. Commented Sep 27, 2022 at 23:46

2 Answers 2

4

I think you already have a good example in your code, where you pass a function in another function, here:

// ABN
$.validator.addMethod('ABN', function(v, element, p) {

Basically, what you need to do is to replace () => with function ():

let total = v.reduce(function(acc, n) {
    return acc + n;
}, 0);
Sign up to request clarification or add additional context in comments.

1 Comment

Caveat: just don't do this blanket across your code. => is not exactly the same as function() (it's fine in this case).
1

var total = v.reduce(function(acc, n) {
    return acc + n;
}, 0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.