Factorial Calculation Recursion
Write a JavaScript function calculateFactorial(n) that calculates the factorial of a given number
recursively. Then, demonstrate how the call stack is used when calling calculateFactorial(5).give
javascript code
Here's a JavaScript function `calculateFactorial(n)` that calculates the factorial of a number recursively:
javascript
function calculateFactorial(n) {
// Base case: factorial of 0 or 1 is 1
if (n === 0 || n === 1) {
return 1;
} else {
// Recursive case: n * factorial of (n-1)
return n * calculateFactorial(n - 1);
}
}
// Testing the function with calculateFactorial(5)
[Link](calculateFactorial(5)); // Output: 120
Explanation of the Call Stack when calling `calculateFactorial(5)`:
1. First Call: `calculateFactorial(5)`
Since `n = 5`, it goes into the recursive case: `5 * calculateFactorial(4)`
The function call `calculateFactorial(4)` is pushed onto the call stack.
2. Second Call: `calculateFactorial(4)`
Since `n = 4`, it goes into the recursive case: `4 * calculateFactorial(3)`
The function call `calculateFactorial(3)` is pushed onto the call stack.
3. Third Call: `calculateFactorial(3)`
Since `n = 3`, it goes into the recursive case: `3 * calculateFactorial(2)`
The function call `calculateFactorial(2)` is pushed onto the call stack.
4. Fourth Call: `calculateFactorial(2)`
Since `n = 2`, it goes into the recursive case: `2 * calculateFactorial(1)`
The function call `calculateFactorial(1)` is pushed onto the call stack.
5. Fifth Call: `calculateFactorial(1)`
Since `n = 1`, it reaches the base case and returns `1`.
6. Returning from Calls:
Now, the function starts returning from the call stack:
`calculateFactorial(1)` returns `1`.
`calculateFactorial(2)` returns `2 * 1 = 2`.
`calculateFactorial(3)` returns `3 * 2 = 6`.
`calculateFactorial(4)` returns `4 * 6 = 24`.
`calculateFactorial(5)` returns `5 * 24 = 120`.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/4
The final result is `120`, which is the factorial of 5.
Visualizing the Call Stack:
1. `calculateFactorial(5)` → `calculateFactorial(4)` → `calculateFactorial(3)` →
`calculateFactorial(2)` → `calculateFactorial(1)`
2. `calculateFactorial(1)` returns `1`
3. `calculateFactorial(2)` returns `2`
4. `calculateFactorial(3)` returns `6`
5. `calculateFactorial(4)` returns `24`
6. `calculateFactorial(5)` returns `120`
Each recursive call is placed on the call stack, and once the base case is reached, the stack starts to
unwind, returning values back through each previous function call.
Given an array of transactions, write a function calculateBalance(transactions) that calculates the
total balance. Use array methods (map, reduce, filter) to implement the [Link] javascript code
Here's a JavaScript function `calculateBalance(transactions)` that calculates the total balance using
array methods like `map`, `reduce`, and `filter`:
JavaScript Code:
javascript
function calculateBalance(transactions) {
// Using filter to ensure we only consider valid transaction amounts (positive or negative numbers
const validTransactions = [Link](transaction => typeof transaction === 'number');
// Using reduce to sum up all the valid transaction amounts
const totalBalance = [Link]((total, current) => total + current, 0);
return totalBalance;
}
// Example usage:
const transactions = [100, -50, 200, -30, 'invalid', 50, 25, 'text'];
[Link](calculateBalance(transactions)); // Output: 295
Explanation:
1. `filter`: We first use the `filter` method to ensure that only valid numbers are considered for
the balance calculation. This removes any non-numeric entries (like strings).
In the example, `['invalid', 'text']` are filtered out.
2. `reduce`: After filtering, we use `reduce` to accumulate the sum of the valid transactions. The
`reduce` function iterates through the array and adds each value to the running total, starting from
`0`.
3. Result: The total balance after processing all valid transactions is returned.
Example:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/4
Given the `transactions` array:
javascript
[100, -50, 200, -30, 'invalid', 50, 25, 'text']
1. After filtering, we get:
javascript
[100, -50, 200, -30, 50, 25]
2. After reducing (summing the values):
javascript
100 + (-50) + 200 + (-30) + 50 + 25 = 295
Thus, the output will be `295`.
Write a function discount(discountPercentage) that returns another function to calculate the
discounted price of a product give javascript code
Here's a JavaScript function `discount(discountPercentage)` that returns another function to calculate
the discounted price of a product:
JavaScript Code:
javascript
function discount(discountPercentage) {
// The returned function calculates the discounted price
return function(price) {
const discountAmount = (price * discountPercentage) / 100;
const discountedPrice = price - discountAmount;
return discountedPrice;
};
}
// Example usage:
const applyDiscount = discount(20); // 20% discount
const discountedPrice = applyDiscount(100); // Original price 100
[Link](discountedPrice); // Output: 80
Explanation:
1. Outer Function (`discount`):
The `discount` function accepts a `discountPercentage` as its argument (e.g., 20%).
It returns another function that will take the original price of the product.
2. Returned Function:
The returned function takes the original price of the product as an argument (`price`).
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/4
It calculates the discount amount by multiplying the price by the discount percentage (divided
by 100).
It subtracts the discount amount from the original price to get the discounted price.
The discounted price is returned.
Example:
If we apply a 20% discount to a product priced at 100, the `applyDiscount(100)` call returns `80`,
which is the discounted price.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/4