tags:

views:

90

answers:

2

I have an onchange event that updates a form, and in the updating process it calls a function to calculate shipping. I'm not sure why, but I'm getting the following error when I try to call the function:

Uncaught TypeError: number is not a function

The function, shipping, looks like this:

function shipping( weight )
{
    var flat

    switch( weight )
    {
        case 1:
        case 2:
        case 3:
            flat = 32.00;
            break;

        case 4:
            flat = 18.50;
            break;

        case 5:
            flat = 15.80;
            break;

        case 6:
            flat = 14.00;
            break;

        case 7:
            flat = 12.71;
            break;

        case 8:
            flat = 11.75;
            break;

        case 9:
            flat = 11.00;
            break;

        case 10:
            flat = 10.40;
            break;

        case 11:
            flat = 9.91;
            break;

        case 12:
            flat = 9.50;
            break;

        case 13:
            flat = 9.15;
            break;

        case 14:
            flat = 8.86;
            break;

        case 15:
            flat = 8.86;
            break;

        case 16:
            flat = 8.38;
            break;

        case 17:
            flat = 8.18;
            break;

        case 18:
            flat = 8.00;
            break;

        case 19:
            flat = 7.84;
            break;

        case 20:
            flat = 7.70;
            break;

    } // switch

    var flat_fee = flat * weight;
    var mile_fee = distance * 0.90;

    var shipping_fee = flat_fee + mile_fee;
    simpleCart.shippingTest = shipping_fee;
    return shipping_fee;
} // shipping

I'm passing in 1 right now. The variable distance is coming from an ajax call that is completed before this function is run. That function looks like this:

function get_distance( zip )
{

    $.getJSON(
        'distance.php',
        { zip:zip },
        function(json)
        {
            distance = json 
        })

} // get_distance

I've checked to make sure the variable distance is set.

The console says the uncaught type error is happing at the line where I call shipping(1). Any thoughts as to why that's happening?

+3  A: 

Are you using shipping as a variable anywhere? Sounds like the function shipping is getting overwritten by using it as a variable with a numeric value of 1.

It's not in the code you posted (neither is the call to shipping(1) you mentioned).

Sam
That was it! Ugh stupid mistakes. Thank you!
hookedonwinter
A: 

You're taking shipping()'s argument and pulling a switch on it, but for cases 1,2 & 3 you aren't assigning a value to $flat. Note that $flat is already declared above the switch with a default value of .. null

switch( weight )
{
    case 1:
    case 2:
    case 3:
        flat = 32.00;
        break;

    case 4:
        flat = 18.50;
        break;

Later on you try to do a mathematical statement with it

var flat_fee = flat * weight;

Yet $flat has no numerical value to do math with, throwing a TypeError.

Zane Edward Dockery
It was actually that I was using shipping as a variable and a function. What I'm doing here with 1, 2, and 3 is actually a switch shortcut.
hookedonwinter