• Resolved justin_19

    (@justinsmith19)


    Hi there

    I am currently working on an assessment-type form, which has the letters A, B, C, D, and E as answers in the form of radio buttons. At the end of the form, using a calculated field, we need to tally up how many times A, B, C, D, and E were selected.

    I’ve created a calculated field for each letter (A, B, C, D, E) and tried using the following equation to tally up the letters.

    (function(){
    
    if(fieldname3 == "A") return 1;
    else return 0;
    
    })();

    However, I realised that I need to increment something every time a letter is clicked. Is there a code that can increment a value? Is there perhaps a better way to achieve this?

    I would greatly appreciate your assistance with this.

    Regards

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author codepeople

    (@codepeople)

    Hello @justinsmith19

    Your website is private. So, I’ll try to describe the process with a hypothetical example.

    Assuming your questions are the radio buttons fields, fieldname1, fieldname2, fieldname3, and fieldname4, where the questions are the fields’ labels, and the choices are the possible answers with the values A, B, C, D, and E

    To calculate the number of A, B, …, and E choices selected, you can implement an equation similar to:

    
    (function(){
        var answers = [fieldname1, fieldname2, fieldname3, fieldname4], 
            A = 0, B = 0, C = 0, D = 0, E = 0;
    
        for(var i in answers)
        {
            if(answers[i] == 'A') A++;
            if(answers[i] == 'B') B++;
            if(answers[i] == 'C') C++;
            if(answers[i] == 'D') D++;
            if(answers[i] == 'E') E++;
        }
    
        return 'You have selected '+A+' A answers, '+B+' B answers, '+C+' C answers, '+D+' D answers, '+E+' E answers';
    })()
    

    Best regards.

    Thread Starter justin_19

    (@justinsmith19)

    It works perfectly!

    Thanks so much!!

    • This reply was modified 3 years, 2 months ago by justin_19.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Calculated options’ is closed to new replies.