javascript if else statement help

Home Forums Quform WordPress javascript if else statement help

This topic is: resolved
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #28213
    aaron1988
    Participant

    Hi Ally,

    I have a field called: Writing and another called: Estimate Overall, i need to do something like:

    If Writing is 6.5 or Less then do Writing = overall score – 0.5

    and if writing is greater than 6.5 then do: writing = overall score – 0.75

    Writing is a checkbox field. and estimate overall is just a text based.

    Hope this makes sense 🙂

    Thanks Aaron

    #28227
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #28229
    aaron1988
    Participant

    Thanks Ally,

    I think i understand :).

    This is my full JS code so you can see what we are doing 🙂

    // Start Yes
    
    jQuery(function ($) {
        var calculate = function () {
            var total = 0;
    
            var listening = $('.quform-field-1_18:checked').val();
            if (listening && $.isNumeric(listening)) {
                total += parseFloat(listening);
            }
    
            var reading = $('.quform-field-1_19:checked').val();
            if (reading && $.isNumeric(reading)) {
                total += parseFloat(reading);
            }
    
            var writing = $('.quform-field-1_20:checked').val();
            if (writing && $.isNumeric(writing)) {
                total += parseFloat(writing);
            }
    
            var speaking = $('.quform-field-1_21:checked').val();
            if (speaking && $.isNumeric(speaking)) {
                total += parseFloat(speaking);
            }
    
           total = Math.round(total * 2) / 2;
    
            // Display the result to the user
            $('#form-total').text('Total: ' + total);
    
            // Total field
            $('.quform-field-1_69').val(total);
    
            var overall = $('.quform-field-1_11:checked').val();
            var target = $('.quform-field-1_23:checked').val();
            var gap = target - overall;
    
            if(gap < 0){
                gap = 0;
            }
            
            //GAP
            $('.quform-field-1_70').val(gap);
    
        };
    
        // Calculate on page load
        calculate();
    
        // Recalculate when these text input fields are changed
        $('.quform-field-1_11, .quform-field-1_18, .quform-field-1_19, .quform-field-1_20, .quform-field-1_21, .quform-field-1_23').on('click', calculate);
    });
    
    jQuery(function ($) {
     $('.quform-input-1_5 input').blur(function () { 
            $('#name').text($('.quform-field-1_5').val() + ' ' + $('.quform-field-1_10').val());
       });
     
        $('.quform-field-1_22').blur(function () {
            $('#taken').text($(this).val());
        });
     
        $('.quform-field-1_18').change(function () {
            $('#listening').text($(this).val());
        });
        $('.quform-field-1_19').change(function () {
            $('#reading').text($(this).val());
        });
    $('.quform-field-1_20').change(function () {
            $('#writing').text($(this).val());
        });
    $('.quform-field-1_21').change(function () {
            $('#speaking').text($(this).val());
        });
    $('.quform-field-1_11').change(function () {
            $('#overall').text($(this).val());
        });
    $('.quform-field-1_23').change(function () {
            $('#overall_band').text($(this).val());
        });
    });
    
    // End Yes
    
    // Start No
    jQuery(function ($) {
        var calculate = function () {
            var total = 0;
            var listeningvalue = 0;
            var readingvalue =  0;
            var targetvalue = 0;
    
            var listening = $('.quform-field-6_18:checked').val();
            if (listening && $.isNumeric(listening)) {
                total += parseFloat(listening);
           listeningvalue = parseFloat(listening);
            }
    
            var reading = $('.quform-field-6_19:checked').val();
            if (reading && $.isNumeric(reading)) {
                total += parseFloat(reading);
            readingvalue += parseFloat(reading);
            }
          
            var target = $('.quform-field-6_22:checked').val();
            if (target && $.isNumeric(target)) {
            targetvalue += parseFloat(target);
            }
    
            var writing = listeningvalue + readingvalue
            writing = writing / 2
            var country = $('.quform-field-6_91:checked').val();
            writing = writing - country
           
    
            writing = Math.round(writing * 2) / 2;
    
            $('.quform-field-6_84').val(writing);
           
           $('.quform-field-6_85').val(listeningvalue);
           
           total += listeningvalue + writing
    
            //Round total to either .0 or .5
          //  total = Math.round(total * 2) / 2;
    
            // Display the result to the user
            $('#form-total').text('Total ' + total);
    
            // Total field
            $('.quform-field-6_83').val(total);
    
         var estoverall = 0;
         estoverall = total / 4;
         estoverall = Math.round(estoverall * 2) / 2;
        $('.quform-field-6_86').val(estoverall);
    
       var gap = 0;
       var gap = targetvalue - estoverall;
    
            if(gap < 0){
                gap = 0;
            }
    
       $('.quform-field-6_87').val(gap);
    
         };
        // Calculate on page load
        calculate();
    
        // Recalculate when these text input fields are changed
        $('.quform-field-6_18, .quform-field-6_19, .quform-field-6_22, .quform-field-6_91').on('click', calculate);
    });
    
    // End No
    
    #28231
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    • This reply was modified 5 years, 2 months ago by Ally. Reason: Added parseFloat
    #28236
    aaron1988
    Participant

    Hi Ally,

    Forgot to say the writing field is not a checkbox my mistake so would i just change the top var writing to be:

    var writing = $(‘.quform-field-6_84).val();

    Thanks
    Aaron

    #28247
    aaron1988
    Participant

    Also in the code i sent i have comment out the stuff to do with country as we wont be used that to do the -0.5 or -0.75 now. where abouts in my code would i put the above so it calculates it correctly?

    Thanks
    Aaron

    #28285
    aaron1988
    Participant

    Sorry to reply to post Ally wondered if you had an update, on where i can put the code so it works as expected – the new piece 🙂

    Thanks
    Aaron

    #28289
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #28290
    aaron1988
    Participant

    Ok Ally,

    Let me have a thing and try and get explanation to you so you know what i am trying to accomplish.

    Thanks
    Aaron

    #28306
    aaron1988
    Participant

    Hi Ally,

    So this is what i need to do 🙂

    i have 2 checkboxes :).

    Listening and Reading so the first calculation is this we need to get the score for “Writing” which is not present except but will be a hidden field.
    So the calculation i need to do is: if overall band score is <= 6.5 do -0.5 if its > 6.5 -0.75
    Now for the second calculation i need to do:
    Listening + Reading, the listening = speaking.
    then: Listening + Reading + Writing + Speaking
    Overall Band Score = Listening + Reading + Writing + Speaking / 4

    I going to try and work on it, but would be good to have some pointers 🙂 as i am new to javascript so learning as i go.

    Also i appreciate all your help.

    Thanks
    Aaron

    #28333
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy