3.12 Calling Procedures JavaScript Hacks
Categories: Javascript3.12 Calling Procedures JS hacks
3.12 Calling Procedures Homework in JavaScript
Complete the 4 tasks below as homework:
- Create the functions
- Run the Code cell
- Have fun!
Hack — Example 1: Simple function with no parameters
%%js
// TODO: Call sayHello to print "Hello there!"
function sayHello() {
console.log("Hello there!")
}
// sayHello();
sayHello();
<IPython.core.display.Javascript object>
- Does the code run as expected?
- If yes, move on to the next hack.
Hack — Example 2: Function with one parameter
%%js
// TODO: Call greet with your name to welcome the student
function greet(name) {
console.log("Hello," + name + "!");
}
greet("David", "B");
<IPython.core.display.Javascript object>
- Does your code properly greet the person?
- Are there no errors in your code?
- If yes, move on to the next hack.
Hack — Example 3: Function with two parameters
%%js
// Function to add two numbers
function addNumbers(a, b) {
return a + b;
}
// Call the function with two numbers and print the result
console.log(addNumbers(3, 7)); // 10
<IPython.core.display.Javascript object>
- Does addNumbers(3, 7) give 10?
- Does it work for other numbers too?
- If yes, move on to the next hack.
Hack — Example 4: Function that returns a value
%%js
// Implement factorial(n) to return n!
function factorial(n) {
if (n === 0 || n === 1) {
return 1; // base case
} else {
return n * factorial(n - 1); // recursive call
}
}
// Example: factorial(5) should return 120
result = factorial(5);
console.log(result); // 120
// Example: factorial(0) should return 1
result = factorial(0);
console.log(result); // 1
// Example: factorial(1) should return 1
result = factorial(1);
console.log(result); // 1
<IPython.core.display.Javascript object>
- Does factorial(0) return 1?
- Does factorial(1) return 1?
- Does factorial(5) return 120?
- If yes, move on to the next hack.
Hack - Challenge Example 5: Function calling another function
%%js
// Dictionary (object) of items and their prices
const storeItems = {
apple: 2.0,
banana: 1.5,
milk: 3.25,
bread: 2.0
};
// Function 1: calculate subtotal
function getSubtotal(cart) {
let subtotal = 0;
for (let item in cart) {
if (storeItems[item]) {
subtotal += storeItems[item] * cart[item];
}
}
return subtotal;
}
// Function 2: add tax
function addTax(subtotal, taxRate) {
return subtotal * (1 + taxRate);
}
// Function 3: checkout
function checkout(cart, taxRate) {
const subtotal = getSubtotal(cart);
const total = addTax(subtotal, taxRate);
console.log(`Subtotal: $${subtotal.toFixed(2)}`);
console.log(`Total (with tax): $${total.toFixed(2)}`);
}
// Example cart for testing
const myCart = {
apple: 2,
milk: 1,
bread: 1
};
// Call checkout with 8% tax
checkout(myCart, 0.08);
<IPython.core.display.Javascript object>
- If the shopping cart contains 2 apples, 1 milk, and 1 bread, will your program show a subtotal of $8.00 and a total of $8.64 with an 8% tax rate?
- Try changing the cart to 3 bananas and 2 milk — should the subtotal be $10.25 and the total with 8% tax be $11.07?
- If your output matches these totals, your function calls and calculations are correct!