-1

I am very new to Javascript. Any help to solve the problem below will be appreciated.

Question

declaring 40+ variables in order to calculate their sum for an HCL Leap form I am building.

My formula

z= variable1+variable2+...+variable 40

Is there a way to declare all the variables inside an array and calculate the sum of that array instead of manually adding the variables.

2
  • 2
    Yes, you can probably use an array instead, but it's hard to say more without seeing more details Commented Feb 6, 2020 at 9:34
  • Here is my current code looks var x = BO.F_Number47.getValue(); var y = BO.F_Number47.getValue(); var z = BO.F_Number47.getValue(); I am now calculating the total for the fields above and setting it to field 119 BO.F_Number119.setValue(x+y+c); Since I have more than 40 fields, I was wondering if I can declare all the fields within an array and calculate the sum. Thanks again for helping. Commented Feb 6, 2020 at 9:38

1 Answer 1

1

Yes, you can put the values (not variables) in an array.

const array = [];
array.push(1);
array.push(2);
// ...

const sum = array.reduce((sum, value) => sum + value, 0);

Here is my current code looks like var x = BO.F_Number47.getValue();

Based on that, you may not need an array, you can do the sum directly using the values from getValue:

let sum = 0;
for (let n = startNumber; n <= endNumber; ++n) {
    sum += BO["F_Number" + n].getValue();
}

For instance, if you wanted the sum of F_Number1 through F_Number47 (inclusive):

let sum = 0;
for (let n = 1; n <= 47; ++n) {
    sum += BO["F_Number" + n].getValue();
}

That uses brackets syntax to access F_Number, F_Number2, etc. See this question's answers.

But you may want to refactor and use an array for F instead:

const BO = {
    F: []
};

...and then put the object in F; this question's answers talk about doing that.


In a comment you've written:

The F_Number is the inbuilt filed Ids assigned that application assigns to the form if it is a number field. So in order to sum the total of all the field values, I have to refer to the field ids. Also the Ids are not in the sequential order like the first field Id is F_Number1, then F_Number2, F_Number 5.

If you want to sum up the values of all of the F_Number## fields that BO has, regardless of their names, you can do that in a couple of ways.

If the F_Number fields on BO are enumerable, you can use a for-in loop:

let sum = 0;
for (const name in BO) {
    if (name.startsWith("F_Number")) {
        sum += BO[name].getValue();
    }
}

If they aren't enumerable but they are owned by BO (not inherited), you can get the names from Reflect.ownKeys (ES2015+) and use a for-of loop (ES2015+):

let sum = 0;
for (const name of Reflect.ownKeys(BO)) {
    if (name.startsWith("F_Number")) {
        sum += BO[name].getValue();
    }
}

If BO inherits them, you can use Object.getPrototypeOf to go up the prototype chain and check every level:

let sum = 0;
let obj = BO;
while (obj) {
    for (const name of Reflect.ownKeys(obj)) {
        if (name.startsWith("F_Number")) {
            sum += obj[name].getValue();
        }
    }
    obj = Object.getPrototypeOf(obj);
}

Side note: The overwhelmingly-common convention in JavaScript is that variables and property names start with lowercase letters. Constants are sometimes written in all caps, and constructor function start with a single uppercase character, but not typically the names of objects (BO) or properties on them (F_Number47). You can do what you like in your own code, of course, but following common conventions helps when sharing code with others (on the team, or here on SO, etc.).

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you TJ. The F_Number is the inbuilt filed Ids assigned that application assigns to the form if it is a number field. So in order to sum the total of all the field values, I have to refer to the field ids. Also the Ids are not in the sequential order like the first field Id is F_Number1, then F_Number2, F_Number 5..
@Sobhishtp - Blech. :-) Is there any method to the naming? Does BO have (for instance) an F_Number3 that you want to skip? Or does F_Number3 just not exist? Do you want to sum up all F_Number## fields that BO has? I've added a couple of options to the answer for you.
@t-j-walker. I have three columns in the form for three diff. months expenses, like Jan expenses, Feb expenses, March Expenses. For each month, there will be 15-20 number fields to input expenses by day. So the field id is created as soon as the fields are created. Unfortunately the fields were created not in order by month. i.e fields for Jan first then Feb. The F_Number1 is for January day1, but F_Number2 is for March. I want to find the total of all Jan expenses. So I was declaring the field ids for Jan as variables and adding them manually to get the total, but quite tedious work
@Sobhishtp - Are the fields consistent? Is F_Number1 always for January day 1, and so on?
@t-j-Crowder. yes. the field names will be consistent.

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.