Skip to content
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

fixes recursion #945 #2594

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions exercises/concept/pizza-order/.docs/about.md
Original file line number Diff line number Diff line change
@@ -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.
33 changes: 30 additions & 3 deletions exercises/concept/pizza-order/pizza-order.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
/// <reference path="./global.d.ts" />

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
}

/**
Expand All @@ -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');
}