-
Notifications
You must be signed in to change notification settings - Fork 1.2k
completed #728
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
completed #728
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,132 @@ | ||
| // Write your Pizza Builder JavaScript in this file. | ||
| $('document').ready(function () { | ||
|
|
||
| // array to calc final pizza price | ||
| var pizzaTotalPrice = [{ type: '', price: 0 }]; | ||
|
|
||
| // create object containing base pizza price | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be super nice to use a constructor function here! |
||
| pizzaTotalPrice.push({ | ||
| type: "basePizza", | ||
| price: 10 | ||
| }); | ||
|
|
||
| pizzaTotalPrice.push({ | ||
| type: "pepperonniTopping", | ||
| price: 1 | ||
| }); | ||
|
|
||
| pizzaTotalPrice.push({ | ||
| type: "mushroomTopping", | ||
| price: 1 | ||
| }); | ||
|
|
||
| pizzaTotalPrice.push({ | ||
| type: "greenPepperTopping", | ||
| price: 1 | ||
| }); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's try our best to avoid duplicate code; normally if you have to repeat the same kind of thing over and over again, its more appropriate to write a function, this is what they are for. Some things to think about when setting up your program:
|
||
| // Total value Calculation | ||
|
|
||
| var pizzaTotal = pizzaTotalPrice.reduce((a, b) => ({ price: a.price + b.price })); | ||
| $("#total").text("$" + pizzaTotal.price); | ||
|
|
||
| // Pepperoni topping button | ||
|
|
||
| $('.btn-pepperonni').click(function () { | ||
| $('.pep').toggle() | ||
| if ($('.btn-pepperonni').hasClass("active")) { | ||
| $('.price li').eq(0).show(); | ||
|
|
||
| pizzaTotalPrice.push({ | ||
| type: "pepperonniTopping", | ||
| price: 1 | ||
| }) | ||
|
|
||
| } | ||
| else { | ||
| $('.price li').eq(0).hide(); | ||
| pizzaTotalPrice.splice(pizzaTotalPrice.map(function (e) { return e.type; }).indexOf('pepperonniTopping'), 1); | ||
| } | ||
| }); | ||
|
|
||
| // Mushroom topping button | ||
|
|
||
| $('.btn-mushrooms').click(function () { | ||
| $('.mushroom').toggle() | ||
| if ($('.btn-mushrooms').hasClass("active")) { | ||
| $('.price li').eq(1).show(); | ||
|
|
||
| pizzaTotalPrice.push({ | ||
| type: "mushroomTopping", | ||
| price: 1 | ||
| }) | ||
| } | ||
| else { | ||
| $('.price li').eq(1).hide(); | ||
| pizzaTotalPrice.splice(pizzaTotalPrice.map(function (e) { return e.type; }).indexOf('mushroomTopping'), 1); | ||
| } | ||
| }); | ||
|
|
||
| // Green Pepper topping button | ||
|
|
||
| $('.btn-green-peppers').click(function () { | ||
| $('.green-pepper').toggle() | ||
| if ($('.btn-green-peppers').hasClass("active")) { | ||
| $('.price li').eq(2).show(); | ||
| pizzaTotalPrice.push({ | ||
| type: "greenPepperTopping", | ||
| price: 1 | ||
| }) | ||
| } | ||
| else { | ||
| $('.price li').eq(2).hide(); | ||
| pizzaTotalPrice.splice(pizzaTotalPrice.map(function (e) { return e.type; }).indexOf('greenPepperTopping'), 1); | ||
| } | ||
| }); | ||
|
|
||
| // White Sauce topping button | ||
|
|
||
| $('.btn-sauce').click(function () { | ||
| $('.sauce').toggleClass("sauce-white") | ||
| if ($('.btn-sauce').hasClass("active")) { | ||
|
|
||
| $('.price li').eq(3).show(); | ||
| pizzaTotalPrice.push({ | ||
| type: "whiteSauceTopping", | ||
| price: 3 | ||
| }) | ||
| } | ||
| else { | ||
| $('.price li').eq(3).hide(); | ||
| pizzaTotalPrice.splice(pizzaTotalPrice.map(function (e) { return e.type; }).indexOf('whiteSauceTopping'), 1); | ||
| } | ||
| }); | ||
|
|
||
| // Gluten Free Crust button | ||
| $('.btn-crust').click(function () { | ||
| $('.crust-gluten-free').toggleClass("crust") | ||
| if ($('.btn-crust').hasClass("active")) { | ||
|
|
||
| $('.price li').eq(4).show(); | ||
| pizzaTotalPrice.push({ | ||
| type: "GlutenFreeCrust", | ||
| price: 5 | ||
| }) | ||
| } | ||
| else { | ||
| $('.price li').eq(4).hide(); | ||
| pizzaTotalPrice.splice(pizzaTotalPrice.map(function (e) { return e.type; }).indexOf('GlutenFreeCrust'), 1); | ||
| } | ||
| }); | ||
|
|
||
| //Total Price Calculation | ||
|
|
||
| $(".btn").on("click", function () { | ||
| $(this).toggleClass("active"); | ||
|
|
||
| var pizzaTotal = pizzaTotalPrice.reduce((a, b) => ({ price: a.price + b.price })); | ||
| $("#total").text("$" + pizzaTotal.price); | ||
| }); | ||
|
|
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this variable is better named something like
pizzaPrices, since you still have to calculate the total prices later.