Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Cameron Maciel
Cameron Maciel
1,433 Points

Is there a way to simplify this code, so there isnt so much

hello was just wondering if there was more of a simpler way of write this type of code , and if so how

var inputNum_of_Shirts = $('#shirts').val(); var inputNum_of_FColor = $('#colorsOnFront').val(); var inputNum_of_BColor = $('#colorsOnBack').val();

        if (inputNum_of_Shirts <=30 && inputNum_of_FColor == 1 ){
            var shirtsPrice = 3;
            var OneColorPrice= 5.50;
            var totalCost = (inputNum_of_Shirts*parseFloat(shirtsPrice))
            +(parseFloat(OneColorPrice)*inputNum_of_Shirts);
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(OneColorPrice)*inputNum_of_FColor)
            +(parseFloat(OneColorPrice)*inputNum_of_BColor);
    }
        if (inputNum_of_Shirts <=30 && inputNum_of_FColor == 2 ){
            var shirtsPrice = 3;
            var TwoColorPrice= 8.18;
            var totalCost = (inputNum_of_Shirts*parseFloat(TwoColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(TwoColorPrice));

    }
        if (inputNum_of_Shirts >30 <=49 && inputNum_of_FColor == 1 ){
            var shirtsPrice = 3;
            var One2ColorPrice= 4.48;
            var totalCost = (inputNum_of_Shirts*parseFloat(One2ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(One2ColorPrice));

    }
    if (inputNum_of_Shirts >30 <=49 && inputNum_of_FColor == 2 ){
            var shirtsPrice = 3;
            var Two2ColorPrice= 5.33;
            var totalCost = (inputNum_of_Shirts*parseFloat(Two2ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Two2ColorPrice));

    }

    if (inputNum_of_Shirts >30 <=49 && inputNum_of_FColor == 3 ){
            var shirtsPrice = 3;
            var Three2ColorPrice= 6.18;
            var totalCost = (inputNum_of_Shirts*parseFloat(Three2ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Three2ColorPrice));

    }
    if (inputNum_of_Shirts >49 <=99 && inputNum_of_FColor == 1 ){
            var shirtsPrice = 3;
            var One3ColorPrice= 3.05;
            var totalCost = (inputNum_of_Shirts*parseFloat(One3ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(One3ColorPrice));

    }
    if (inputNum_of_Shirts >49 <=99 && inputNum_of_FColor == 2 ){
            var shirtsPrice = 3;
            var Two3ColorPrice= 3.43;
            var totalCost = (inputNum_of_Shirts*parseFloat(Two3ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Two3ColorPrice));

    }
    if (inputNum_of_Shirts >49 <=99 && inputNum_of_FColor == 3 ){
            var shirtsPrice = 3;
            var Three3ColorPrice= 3.90;
            var totalCost = (inputNum_of_Shirts*parseFloat(Three3ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Three3ColorPrice));

    }
        if (inputNum_of_Shirts >49 <=99 && inputNum_of_FColor == 4 ){
            var shirtsPrice = 3;
            var Four3ColorPrice= 4.23;
            var totalCost = (inputNum_of_Shirts*parseFloat(Four3ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Four3ColorPrice));

    }
        if (inputNum_of_Shirts >99 <=199 && inputNum_of_FColor == 1 ){
            var shirtsPrice = 3;
            var One4ColorPrice= 2.15;
            var totalCost = (inputNum_of_Shirts*parseFloat(One4ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(One4ColorPrice));

    }
        if (inputNum_of_Shirts >99 <=199 && inputNum_of_FColor == 2 ){
            var shirtsPrice = 3;
            var Two4ColorPrice= 2.43;
            var totalCost = (inputNum_of_Shirts*parseFloat(Two4ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Two4ColorPrice));

    }
        if (inputNum_of_Shirts >99 <=199 && inputNum_of_FColor == 3 ){
            var shirtsPrice = 3;
            var Three4ColorPrice= 2.78;
            var totalCost = (inputNum_of_Shirts*parseFloat(Three4ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Three4ColorPrice));

    }
        if (inputNum_of_Shirts >99 <=199 && inputNum_of_FColor == 4 ){
            var shirtsPrice = 3;
            var Four4ColorPrice= 3.13;
            var totalCost = (inputNum_of_Shirts*parseFloat(Four4ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Four4ColorPrice));

    }

        if (inputNum_of_Shirts >199 <=499 && inputNum_of_FColor == 1 ){
            var shirtsPrice = 3;
            var One5ColorPrice= 1.68;
            var totalCost = (inputNum_of_Shirts*parseFloat(One5ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(One5ColorPrice));

    }
        if (inputNum_of_Shirts >199 <=499 && inputNum_of_FColor == 2 ){
            var shirtsPrice = 3;
            var Two5ColorPrice= 1.93;
            var totalCost = (inputNum_of_Shirts*parseFloat(Two5ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Two5ColorPrice));

    }
        if (inputNum_of_Shirts >199 <=499 && inputNum_of_FColor == 3 ){
            var shirtsPrice = 3;
            var Three5ColorPrice= 2.18;
            var totalCost = (inputNum_of_Shirts*parseFloat(Three5ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Three5ColorPrice));

    }
        if (inputNum_of_Shirts >199 <=499 && inputNum_of_FColor == 4 ){
            var shirtsPrice = 3;
            var Four5ColorPrice= 2.43;
            var totalCost = (inputNum_of_Shirts*parseFloat(Four5ColorPrice))
            +(inputNum_of_Shirts*parseFloat(shirtsPrice));
            var perShirtCost = (parseFloat(shirtsPrice))
            +(parseFloat(Four5ColorPrice));

    }

3 Answers

First thing I see when I take a look is you don't have to define all the variables in each IF statement. At the very least, just defining var shirtsPrice = 3 at the top and leaving it out of every IF statement would clean it up a bit.

You could also just define each of the different parts one time at the top and then change the values in IF statements depending on how many shirts and in how many colors the customer orders.

I'm a little confused by the two5colorPrice, three5colorPrice, etc., all the different vars and what they correspond to. Basically, you should only have to define your variables once and then write one if statement with else ifs to decide the different scenarios.

shirtsPrice = 3
var totalCost = shirtsPrice + x...
var perShirtCost = x...

if (customer wants 30 or less shirts at one color) 
     {shirtsprice = this
       etc.}
    else if (customer wants 30 or less shirts at two colors)
     {shirtsprice = this
       etc.}

Hope this helped at all :D

Cameron Maciel
Cameron Maciel
1,433 Points

WOOOWW yes thank you so much, i think ill be able to run with this and make it work just right.. .

thank you so much

As Paul pointed out you have a lot of unnecessary repetition here. I suggest you edit your code and repost it.

Hint: Once you have established the user doesn't want 30 shirts or less use a nested if to avoid checking again.

For sure! Glad to have helped :)