Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to calculate summation of square of numbers in array in CFML

I am solving kata on codewars. Simple challenge, but I struggle to debug my CFML code.

Task is to square each number in array numbers and return summation of these squares.

Here is my solution:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

component {
    numeric function squareSum(required Array numbers) {
        numeric result = 0;
        for (numeric i = 1; i <= arrayLen(numbers); i++) {
            result = result + (numbers[i] * numbers[i]);
        }
        return result;
    }
}

and here is the error:

template:Missing [;] or [line feed] after expression:

>Solution :

Coldfusion is not a strongly typed language. So numeric is not needed on the variable definition. But function return type numeric is allowed.

component {
    numeric function squareSum(required Array numbers) {
        result = 0;
        for (i = 1; i <= arrayLen(numbers); i++) {
            result = result + (numbers[i] * numbers[i]);
        }
        return result;
    }
}

DEMO

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading