Sum Multiple Fields

I’m developing a contest page.
I have the 5 fields for each judge that assign points 0 to 5
I need to fill a field with the total point values that is the result of the sum of Judge1+Judge2…Judge5
Some sample code for this?
Thanks in advance.

I wrote this but not good, i can get the result

}
export function PT_viewportEnter(event,$w) {
let J1 = Number;$w(“#J1”).value;
let J2 = Number;$w(“#J2”).value;
let J3 = Number;$w(“#J3”).value;
let J4 = Number;$w(“#J4”).value;
let J5 = Number;$w(“#J5”).value;
$w(“#PT”).text = (J1+J2+J3+J4+J5).string();
}

@J. D. maybe you can help me with this! i wrote this but dosnt work
export function button11_click(event) {
let J1 = Number($w(“#J1”).value);
let J2 = Number($w(“#J2”).value);
let J3 = Number($w(“#J3”).value);
let J4 = Number($w(“#J4”).value);
let J5 = Number($w(“#J5”).value);
$w(“#PT”).text = (J1*J2+J3+J4+J5).toString();
}
Any Clues?

  • you should add a condition to avoid cases where one or more values are “undefined” (If no user input has been inserted.
    And you used J1*J2 instead of J1 + J2

@jonatandor35 But * is not multiplier? i need the sum of the 5 fields to

Can you give me the condition line?

For example, you can make the user input set to “Required”, then:

const inputs  = [$w("#input1"), $w("#input2"), /*etc..*/ ];
$w("#button11").onClick((event) => {
    if(inputs.every(i => i.valid){
        let sum = inputs.reduce((a,c) => a + Number(c.value), 0);
        $w("#PT").text = sum.toString();
    } else {
    console.log("Some inputs are missing");
    }
})

@jonatandor35 Perfect thanks

The imput cant be required, do is a contest and the each imput comes when the judge vote so i need to do something that if the value is 0 or null dot’t afect the sum but do the sum with the partial values

@ricardorosenblatt

const inputs  = [$w("#input1"), $w("#input2"), /*etc..*/ ];
$w("#button11").onClick((event) => {
let sum = 0;
inputs.forEach(i => {
 if (typeof i.value !== "undefined"){sum += Number(i.value);}
})
 $w("#PT").text = sum.toString();
})

@jonatandor35 Thanks very much, now i wrote the script and i got The element selector function (usually $w) cannot be used before the page is ready

@ricardorosenblatt put all of this code inside the $w.onReady() :
https://www.wix.com/corvid/reference/$w.html#onReady

@jonatandor35 This give me parsing error on the line $w unexpected token

$w.onReady() {
const inputs = [$w(“#J1”), $w(“#J2”), $w(“#J3”),$w(“#J4”),$w(“#J5”)];
$w(“#button11”).onClick((event) => {
let sum = 0;
inputs.forEach(i => {
if ( typeof i.value !== “undefined”){sum += Number(i.value);}
})
$w(“#PT”).text = sum.toString();
});
})

Ok Fixed no more error but the sum of values still give me 0

@J. D. this is the final code but the result of the sum is still 0

$w.onReady ( function () {
const inputs = [$w(“#J1”), $w(“#J2”), $w(“#J3”),$w(“#J4”),$w(“#J5”)];
$w(“#PT”).onViewportEnter((event) => {
let sum = 0;
inputs.forEach(i => {
if ( typeof i.value !== “undefined”){sum += Number(i.value);}
})
$w(“#PT”).text = sum.toString();
})
})

@J. D. Thanks for all your valuable help! but in the last code the sum of the value is still give me 0 any clue why? best regards!!!

Why are you using onViewportEnter() ?

@jonatandor35 I asume is the way to execute when the browser load the page and display the result on the text

@ricardorosenblatt I don’t understand it. If it’s a user input then you should calculate it onChange() not onViewportEnter()

@jonatandor35 #PT is a text the usr inputs are J1…J5 so #PT must show the sum fo J’s values. I can change #PT for a input vox but is only a Text