skip to Main Content

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;
            }
        
    });

2

Answers


  1. var total = v.reduce(function(acc, n) {
        return acc + n;
    }, 0);
    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search