3.10 Javascript Hacks
%%js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shopping Cart</title>
<style>
.cart {
font-size: 24px;
}
.cart-item {
display: inline-block;
margin-right: 10px;
}
</style>
</head>
<body>
<h1>Shopping Cart</h1>
<div>
<input type="text" id="itemInput" placeholder="Enter item name">
<button onclick="addItem()">Add to Cart</button>
<button onclick="removeItem()">Remove from Cart</button>
</div>
<h2>Cart:</h2>
<div id="cart" class="cart"></div>
<script>
let cart = [];
// Function to add an item to the cart
function addItem() {
let item = document.getElementById("itemInput").value;
if (item) {
cart.push(item);
document.getElementById("itemInput").value = ''; // clear the input field
displayCart();
} else {
alert("Please enter an item.");
}
}
// Function to remove an item from the cart
function removeItem() {
let item = document.getElementById("itemInput").value;
let index = cart.indexOf(item);
if (index !== -1) {
cart.splice(index, 1);
document.getElementById("itemInput").value = ''; // clear the input field
displayCart();
} else {
alert(`${item} not found in the cart.`);
}
}
// Function to display the cart items
function displayCart() {
let cartDiv = document.getElementById("cart");
cartDiv.innerHTML = ''; // clear the previous items
if (cart.length === 0) {
cartDiv.innerHTML = 'Your cart is empty.';
} else {
cart.forEach(item => {
let itemDiv = document.createElement('div');
itemDiv.className = 'cart-item';
itemDiv.textContent = `🛒 ${item}`;
cartDiv.appendChild(itemDiv);
});
}
}
</script>
</body>
</html>
<IPython.core.display.Javascript object>
3.10 Python Hacks
def find_min_max(lst):
if len(lst) == 0:
return None, None # Handle empty list case
min_val = lst[0]
max_val = lst[0]
for num in lst[1:]:
if num < min_val:
min_val = num
if num > max_val:
max_val = num
return min_val, max_val
# Example usage
numbers = [3, 5, 1, 2, 9, 7]
minimum, maximum = find_min_max(numbers)
print(f"Minimum: {minimum}, Maximum: {maximum}")
Minimum: 1, Maximum: 9
Popcorn Hack 1
# Step 1: Create a list of your choosing
my_list = [10, 20, 30, 40, 50]
# Step 2: Remove the last element or item in the list
my_list.pop() # Removes the last element
print("After removing the last element:", my_list)
# Step 3: Remove the first element or item in the list
my_list.pop(0) # Removes the first element
print("After removing the first element:", my_list)
# Step 4: Remove an element in the list that has a different index than the first or last element
my_list.pop(1) # Removing an element in the middle (index 1)
print("After removing an element in the middle:", my_list)
# Step 5: Print the final list
print("Final list:", my_list)
After removing the last element: [10, 20, 30, 40]
After removing the first element: [20, 30, 40]
After removing an element in the middle: [20, 40]
Final list: [20, 40]
Popcorn Hack 2
%%js
// Create an array
let arr = [1, 2, 3, 4, 5];
// Reverse the array in place
for (let i = 0; i < Math.floor(arr.length / 2); i++) {
let temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}
// Print the reversed array
console.log("Reversed array:", arr);
<IPython.core.display.Javascript object>