Skip to content

Commit d4b91b3

Browse files
add recersive function
1 parent 2c66786 commit d4b91b3

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

exercises/concept/pizza-order/pizza-order.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
/// <reference path="./global.d.ts" />
2+
3+
import { forEach } from "core-js/core/array"
4+
25
//
36
// @ts-check
47

58
/**
69
* Determine the price of the pizza given the pizza and optional extras
710
*
8-
* @param {Pizza} pizza name of the pizza to be made
11+
* @param {Pizz a} pizza name of the pizza to be made
912
* @param {Extra[]} extras list of extras
1013
*
1114
* @returns {number} the price of the pizza
1215
*/
1316
export function pizzaPrice(pizza, ...extras) {
14-
throw new Error('Please implement the pizzaPrice function');
17+
const PizzaPrice = {
18+
Margherita: 7,
19+
Caprese: 9,
20+
Formaggio: 10,
21+
}
22+
const extrasPrice = {
23+
ExtraSauce: 1,
24+
ExtraToppings: 2,
25+
}
26+
let price = PizzaPrice[pizza]||0
27+
for( const extra of extras){
28+
price += extrasPrice[extra]||0
29+
}
30+
return price
1531
}
1632

1733
/**
@@ -24,5 +40,16 @@ export function pizzaPrice(pizza, ...extras) {
2440
* @returns {number} the price of the total order
2541
*/
2642
export function orderPrice(pizzaOrders) {
27-
throw new Error('Please implement the orderPrice function');
43+
if(pizzaOrders.length === 0){
44+
return 0;
45+
}
46+
47+
let totalPrice = 0
48+
49+
pizzaOrders.forEach(order => {
50+
totalPrice += pizzaPrice(order.pizza, ...order.extras)
51+
})
52+
53+
return totalPrice
54+
// throw new Error('Please implement the orderPrice function');
2855
}

0 commit comments

Comments
 (0)