locked
Help debugging function RRS feed

  • Question

  • User-173651909 posted

    Can someone help me find out why this function is completing? Here's the code:

      function countTotals(myvalue)
        {
            var sumA = 0;
            var sumB = 0;
            var maxA = @ViewBag.MaxA;
            var maxB = @ViewBag.MaxB;
            var itemA = 0;
            var itemB = 0;
            var values;
        
            $('.' + myvalue).each(function () {
                if($(this).is(':checked')){
                    // split string and check weight and power
                    values = ($(this).val()).split("/");       
                    itemA = parseFloat(values[0]);
                    itemB = parseFloat(values[1]);
                    // increment sum
                    sumA += itemA;
                    sumB = itemB;
                }
    
            });
            alert('sumA: ' + sumA + ', maxA: ' + maxA);
            if (sumA > maxA) {
                alert('in if statement');
            }
        }

    The function is called by a radiobutton onclick passing a value. The value passed can be 1 to 6 and is a classed use to group the radiobuttons. The function looks at each radiobutton in the group and if one is checked then it splits the string and assigns the respective values to itemA and itemB. It then uses those values to increment a total (sumA and sumB).

    The code works until I get to the if statement at the bottom which checks if the sum is greater than the max. The alert before the if statement executes successfully but it doesn't get further even if I say if (288 > 4). Any ideas why this might be?

    Thanks in advance
    Adam

    Thursday, January 21, 2016 12:22 PM

Answers

  • User475983607 posted

    Check the types being compared in the conditional statement.

    '288' > '4' = false
    '288' > 4 = true
    288 > '4' = true
    288 > 4 = true

    I assume maxA is a string which cause JavaScript to casts 288 to a string so it can make the comparison.  

    Remember, you have a great resource in developer tools (F12).  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 21, 2016 12:57 PM