diff --git a/exercises/concept/pizza-order/.docs/about.md b/exercises/concept/pizza-order/.docs/about.md new file mode 100644 index 0000000000..67ac0f6694 --- /dev/null +++ b/exercises/concept/pizza-order/.docs/about.md @@ -0,0 +1,9 @@ +### About Recursion + +Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. It’s a powerful tool for breaking down problems that can be divided into smaller, similar sub-problems. However, recursion can quickly run into issues such as **Memory Allocation Errors** or **Maximum Call Stack Size Exceeded** if not handled carefully. + +In JavaScript, one important limitation is the **lack of Tail-Call Optimization (TCO)**. Tail-call optimization allows the language engine to reuse the stack frame for recursive calls when they occur at the end of a function. Unfortunately, JavaScript does not support TCO, which means that deeply nested recursive calls can lead to a stack overflow. + +### Interesting Facts: +- Recursion is widely used in problems such as tree traversal, factorial calculation, and Fibonacci sequences. +- JavaScript does not optimize tail calls, unlike other languages such as Scheme or Haskell. diff --git a/exercises/concept/pizza-order/pizza-order.js b/exercises/concept/pizza-order/pizza-order.js index 4f0ec737f9..c4085789ab 100644 --- a/exercises/concept/pizza-order/pizza-order.js +++ b/exercises/concept/pizza-order/pizza-order.js @@ -1,17 +1,33 @@ /// + +import { forEach } from "core-js/core/array" + // // @ts-check /** * Determine the price of the pizza given the pizza and optional extras * - * @param {Pizza} pizza name of the pizza to be made + * @param {Pizz a} pizza name of the pizza to be made * @param {Extra[]} extras list of extras * * @returns {number} the price of the pizza */ export function pizzaPrice(pizza, ...extras) { - throw new Error('Please implement the pizzaPrice function'); + const PizzaPrice = { + Margherita: 7, + Caprese: 9, + Formaggio: 10, + } + const extrasPrice = { + ExtraSauce: 1, + ExtraToppings: 2, + } + let price = PizzaPrice[pizza]||0 + for( const extra of extras){ + price += extrasPrice[extra]||0 + } + return price } /** @@ -24,5 +40,16 @@ export function pizzaPrice(pizza, ...extras) { * @returns {number} the price of the total order */ export function orderPrice(pizzaOrders) { - throw new Error('Please implement the orderPrice function'); + if(pizzaOrders.length === 0){ + return 0; + } + + let totalPrice = 0 + + pizzaOrders.forEach(order => { + totalPrice += pizzaPrice(order.pizza, ...order.extras) + }) + + return totalPrice + // throw new Error('Please implement the orderPrice function'); }