1

I have a calculation script that gets its number from a JSON and I added some deductions if they order a certain amount of pieces but I can't get it to change when a different garment is selected here is my code for the JSON:

       var shirtsJSON = [
            {"pattern": "Delta Adult S/S 5.2oz", "basePrice": 5.68, 
                "sizes": {"s-xl": 0, "xxl": 0.84}, 
                "colors": {"white": 0, "athletic": 0.12, "color": 0.23}, 
                "numColors": {"1-2": 0, "3-4": 1.60, "5-6": 3.18, "7-8": 4.81, "9-10": 6.39}, 
                "deductions": {"de48pp": 0, "de72pp": .90, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65},
                "oneLocation": {"onelocnone": 0, "oneloc12": 3.28, "oneloc34": 5.41, "oneloc56": 7.52, "oneloc78": 9.69, "oneloc910": 11.80}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 3.28, "twoloc34": 5.41, "twoloc56": 7.52, "twoloc78": 9.69, "twoloc910": 11.80},
                "threeLocation": {"threelocnone": 0, "threeloc12": 3.28, "threeloc34": 5.41, "threeloc56": 7.52, "threeloc78": 9.69, "threeloc910": 11.80},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 3.28, "fourloc34": 5.41, "fourloc56": 7.52, "fourloc78": 9.69, "fourloc910": 11.80}},

            {"pattern": "Delta Adult S/S 6.1oz", "basePrice": 5.68, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 0, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 3.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Gildan Adult S/S 5.4oz", "basePrice": 1.21, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "onelocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Gildan Adult S/S 6.1oz", "basePrice": 1.31, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Delta Adult 5.5oz Ringspung Tee", "basePrice": 2.48, 
                "sizes": {"s-xl": 0, "xxl": 3}, "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "AIstyle Adult 5.5oz Soft Cotton Tee", "basePrice": 2.40, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Next Level Mens Fitted S/S Tee", "basePrice": 2.76, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Delta Adult 5.2oz L/S Tee", "basePrice": 2.35, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "AIstlye Adult 6oz L/S Tee", "basePrice": 2.70, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}},

            {"pattern": "Gildan Adult 5.4oz L/S Tee", "basePrice": 3.30, 
                "sizes": {"s-xl": 0, "xxl": 3}, 
                "colors": {"white": 0, "athletic": 0, "color": 1.13}, 
                "numColors": {"1-2": 1, "3-4": 2, "5-6": 3, "7-8": 4, "9-10": 5}, 
                "deductions": {"de48pp": 0, "de72pp": 1.20, "de96pp": 2.35, "de144pp": 1.37, "de288pp": 2.65}, 
                "oneLocation": {"onelocnone": 0, "oneloc12": 1.60, "oneloc34": 3.18, "oneloc56": 4.81, "oneloc78": 6.39, "oneloc910": 6.39}, 
                "twoLocation": {"twolocnone": 0, "twoloc12": 1.60, "twoloc34": 3.18, "twoloc56": 4.81, "twoloc78": 6.39, "twoloc910": 6.39},
                "threeLocation": {"threelocnone": 0, "threeloc12": 1.60, "threeloc34": 3.18, "threeloc56": 4.81, "threeloc78": 6.39, "threeloc910": 6.39},
                "fourLocation": {"fourlocnone": 0, "fourloc12": 1.60, "fourloc34": 3.18, "fourloc56": 4.81, "fourloc78": 6.39, "fourloc910": 6.39}}];

Then here is where it calculates

            for (i = 1; i <= numOptions; i++) {
                if ($('#option' + i + 'check').is(':checked')) {
                    totalPrice += parseFloat($('#option' + i + 'check').val());
                }
            }

            for(i = 0; i < shirtsJSON.deductions; i++) {
            }

            /**
            * This adds the final prices into the results area
            */
            $('#48pc').html("48pc : " + currency + getMoney(totalPrice * 48));
            $('#72pc').html("72pc : " + currency + getMoney(totalPrice * 72 ));
            $('#96pc').html("96pc : " + currency + getMoney(totalPrice * 96));
            $('#144pc').html("144pc : " + currency + getMoney(totalPrice * 144));
            $('#288pc').html("288pc : " + currency + getMoney(totalPrice * 288));


            $('#48pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de48pp));
            $('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de72pp));
            $('#96pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de96pp));
            $('#144pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de144pp));
            $('#288pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[i].deductions.de288pp));
        }

        //-->

Right now it just draws the first pattern of deductions but I want it to change with each shirt. I added the loop in it but it still just does the first set.

Here is the entire code if you want to look at it. https://gist.github.com/1719315

8
  • did you try dumping the value via console.log and try to see what's happening?.. Commented Feb 7, 2012 at 19:31
  • Can't really tell what you are asking about...but there are several issues I do see...first of all, HTML spec defines that an element's ID MUST begin with A-Z or a-Z, then can use numbers and underscores. Second, I would do all the price calculations on the server side. Doing it on the browser side could very well lead to someone injecting false prices and getting items from you for free basically. Instead, send the content to display in JSON, and if you have to update, send a request to the server for the new data. Commented Feb 7, 2012 at 19:35
  • This wont be for us it will just be for the customer to see... So if they customer is changing the pricing its not going to help them. I'm still learning the JSON and Javascript. I'm just trying to get it so when the person selects their tshirt that it applies those deductions which are discounts to the 48pc, 72pc, 96pc, 144pc, 288pc. Each shirt will have different prices. Commented Feb 7, 2012 at 19:42
  • I know there is an error where the var shirtsJSON = [ is suppose to be var shirtsJSON = { but when I change it, it breaks the code. I'm just trying to figure out how to get my deductions to pull into my calculations. Commented Feb 7, 2012 at 20:05
  • You dont necessarily need to do that, you need a 0 in front of that .9 though, otherwise it will interpret it as an unquoted string Commented Feb 7, 2012 at 20:07

2 Answers 2

1

It's only doing one calculation because the deductions code isn't in the for loop. Look at your curly braces.

Note that the scope of your variable i is the calculatePrices function. Declaring a variable in a for loop header doesn't scope that variable to the for loop, but to the for loop's parent function. So you're reusing i in several places. This is why your use of it outside a for loop isn't throwing an error.

UPDATED Try this (using sIndex instead of i)

function calculatePrices() {
    totalPrice = 0.00;
    for (var i = 1; i <= numShirts; i++) {
        sIndex = parseInt($('#shirt' + i + 'pattern').val());
        size = $('#shirt' + i + 'size').val();
        color = $('#shirt' + i + 'color').val();
        numColors = $('#shirt' + i + 'numColors').val();
        oneLocation = $('#shirt' + i + 'oneLocation').val();
        twoLocation = $('#shirt' + i + 'twoLocation').val();
        threeLocation = $('#shirt' + i + 'threeLocation').val();
        fourLocation = $('#shirt' + i + 'fourLocation').val();

        price = shirtsJSON[sIndex]["basePrice"];
        price += shirtsJSON[sIndex]["sizes"][size];
        price += shirtsJSON[sIndex]["colors"][color];
        price += shirtsJSON[sIndex]["numColors"][numColors];
        price += shirtsJSON[sIndex]["oneLocation"][oneLocation];
        price += shirtsJSON[sIndex]["twoLocation"][twoLocation];
        price += shirtsJSON[sIndex]["threeLocation"][threeLocation];
        price += shirtsJSON[sIndex]["fourLocation"][fourLocation];

        totalPrice += price;


        /**
        * This goes over the selected options, and adds additional prices if any selected
        * Not sure that using totalPrice for this here is the best strategy. 
        * It works if you have 1 shirt total. But when you add a mechanism to 
        * let users pick a 2nd shirt, it will only take the checked options
        * of the last shirt into account. Maybe add the extra options from
        * each shirt to a global variable, which gets added into the total
        * in calculatePrices.
        */
        for (var j = 1; j <= numOptions; j++) {
            if ($('#option' + j + 'check').is(':checked')) {
                totalPrice += parseFloat($('#option' + j + 'check').val());
            }
        }

        /**
        * This adds the final prices into the results area
        */
        $('#48pc').html("48pc : " + currency + getMoney(totalPrice * 48));
        $('#72pc').html("72pc : " + currency + getMoney(totalPrice * 72 ));
        $('#96pc').html("96pc : " + currency + getMoney(totalPrice * 96));
        $('#144pc').html("144pc : " + currency + getMoney(totalPrice * 144));
        $('#288pc').html("288pc : " + currency + getMoney(totalPrice * 288));


        $('#48pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de48pp));
        $('#72pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de72pp));
        $('#96pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de96pp));
        $('#144pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de144pp));
        $('#288pp').html("Per Piece : " + currency + getMoney(totalPrice - shirtsJSON[sIndex].deductions.de288pp));
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What if I did it like this? for (i = 1; i <= numShirts; i++) { sIndex = parseInt($('#shirt' + i + 'pattern').val()); size = $('#shirt' + i + 'size').val(); color = $('#shirt' + i + 'color').val(); numColors = $('#shirt' + i + 'numColors').val(); oneLocation = $('#shirt' + i + 'oneLocation').val(); twoLocation = $('#shirt' + i + 'twoLocation').val(); threeLocation = $('#shirt' + i + 'threeLocation').val(); fourLocation = $('#shirt' + i + 'fourLocation').val(); deductions = $('#shirt' + i - 'deductions').val();
I don't think that is the problem though, he is trying to iterate over the wrong object
Updated my answer after trying your code locally. I think I know what you're after now. You want the deductions from the selected shirt to apply, right?
0

Swap out your code:

        for(i = 0; i < shirtsJSON.deductions; i++) {

        }

for

        $(shirtsJSON).each(function() {

           alert( $(this.deductions).length );

        });

Maybe that will help point you in the correct direction. You are iterating over the incorrect object. Using jQuery's each() function might be what you need to go over that JS Object.

5 Comments

When I did that I had a bunch dialogs just said 1? If I did the each() i would have to rewrite most of it wont I?
You may, but it would probably be a better route than the one you are heading
haha... not really an option at this point. Need to have it done in next few hours.
Shouldn't take you that long :) Unfortunately, I don't have a very clear idea of what you are trying to do
Got it figured out by DaveS. Thanks for looking at it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.