Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement in function

I'm trying to add an if statement to my function, but it's not working. (All the variables have already been declared in my complete code.)

Here's the original code:

function processCheckout() {
    //static paypal request arguments
    var pp_settings = {
        cmd: '_cart',
        upload: 1,
        no_note: 0,
        bn: 'JQPayPalShop_ShoppingCart_EC_US',
        tax: 0,
        rm: 2,
        custom: ''
    };

    //copy settings.paypal to pp_settings 
    $.extend(pp_settings, settings.paypal);

    //create form for POST request
    var form = $('<form />');
    form.attr('action', 'https://www.paypal.com/cgi-bin/webscr');
    form.attr('method', 'post');
    form.attr('target', '_blank');

    //add paypal variables
    var arg;
    for (var key in pp_settings) {
        arg = $('<input type="hidden" />');
        arg.attr('name', key);
        arg.attr('value', pp_settings[key]);
        //add to form
        form.append(arg);
    }

    //now process items in cart
    var item_index = 0;
    //properties map for 'cart' to the paypal variables
    var map = {
        name: 'item_name',
        quantity: 'quantity',
        checkout_price: 'amount',
        shipping: 'shipping',
        number: 'item_number',
        handling: 'handling'
    };

    for (var g in cart) {
        //group
        for (var i in cart[g]) {
            //item
            if (i == 'length')
                continue;
            //skip length property
            item_index++;
            //process item
            for (var k in map) {
                arg = $('<input type="hidden" />');
                arg.attr('name', map[k] + '_' + item_index);
                arg.attr('value', cart[g][i][k]);
                form.append(arg);
            }
        }
    }

    //add form to the document
    shop.append(form);
    form.submit();
    //remove form
    shop.remove(form);
}

And here's the code I tried to modify:

function processCheckout() {
    if (canBuy = false)
    {
        alert("False");
    }
    else
    {
        //static paypal request arguments
        var pp_settings = {
            cmd: '_cart',
            upload: 1,
            no_note: 0,
            bn: 'JQPayPalShop_ShoppingCart_EC_US',
            tax: 0,
            rm: 2,
            custom: ''
        };

        //copy settings.paypal to pp_settings 
        $.extend(pp_settings, settings.paypal);

        //create form for POST request
        var form = $('<form />');
        form.attr('action', 'https://www.paypal.com/cgi-bin/webscr');
        form.attr('method', 'post');
        form.attr('target', '_blank');

        //add paypal variables
        var arg;
        for (var key in pp_settings) {
            arg = $('<input type="hidden" />');
            arg.attr('name', key);
            arg.attr('value', pp_settings[key]);
            //add to form
            form.append(arg);
        }

        //now process items in cart
        var item_index = 0;
        //properties map for 'cart' to the paypal variables
        var map = {
            name: 'item_name',
            quantity: 'quantity',
            checkout_price: 'amount',
            shipping: 'shipping',
            number: 'item_number',
            handling: 'handling'
        };

        for (var g in cart) {
            //group
            for (var i in cart[g]) {
                //item
                if (i == 'length')
                    continue;
                //skip length property
                item_index++;
                //process item
                for (var k in map) {
                    arg = $('<input type="hidden" />');
                    arg.attr('name', map[k] + '_' + item_index);
                    arg.attr('value', cart[g][i][k]);
                    form.append(arg);
                }
            }
        }

        //add form to the document
        shop.append(form);
        form.submit();
        //remove form
        shop.remove(form);
    }
}

I want the whole function to work only if the canBuy variable is equals to true, else, alert("False").

like image 937
larin555 Avatar asked Nov 30 '22 14:11

larin555


2 Answers

// WRONG
if (canBuy = false)

// GOOD
if (canBuy == false)

// BETTER
if (!canBuy)
like image 53
paulsm4 Avatar answered Dec 09 '22 17:12

paulsm4


The new if statement should be using == (comparison) instead of = (assignment)

if (canBuy = false)

Change to...

if (canBuy == false)
like image 34
freefaller Avatar answered Dec 09 '22 19:12

freefaller