//3.10.1 js popcorn hack 1
let aList = [];
let itemsToAdd = ["nora", "kushi", "maryam"];
// Appendding
for (let i = 0; i < itemsToAdd.length; i++) {
aList.push(itemsToAdd[i]);
}
// Use a for loop to print each item
console.log("The items in your list are:");
for (let i = 0; i < aList.length; i++) {
console.log(aList[i]);
}
//3.10.1 exercise: shopping list in js
let shoppingList = [];
while (true) { //user input w infinite loop until unser quits
let userInput = prompt("Enter an item for the shopping list (or type 'q' to quit):");
// type q to quit
if (userInput.toLowerCase() === 'q') {
break;
}
// Append the item to the array
shoppingList.push(userInput);
// Display the current shopping list
console.log("Current shopping list:", shoppingList);
}
// Final display: show the entire shopping list
console.log("Your final shopping list:");
for (let i = 0; i < shoppingList.length; i++) {
console.log(`${i + 1}. ${shoppingList[i]}`);
}
//3.10.2 js popcorn hack
// Initialize an empty array
let aList = [];
// Start an infinite loop for user input
while (true) {
// Prompt the user to enter an item
let userInput = prompt("Enter an item for the list (or type 'q' to quit):");
// If the user types 'q', exit the loop
if (userInput.toLowerCase() === 'q') {
break;
}
// Append the item to the array
aList.push(userInput);
}
// Display the entire list
console.log("Your list:", aList);
// Check if the second element exists
if (aList.length > 1) {
// Display the second element
console.log("Second element in the list is:", aList[1]);
// Remove the second element using splice
aList.splice(1, 1);
// Display the updated list after deleting the second element
console.log("List after removing the second element:", aList);
} else {
console.log("There is no second element");
}
#3.10.3 python popcorn hack
food = ["noodles", "cereal", "bread", "ice cream", "chicken"]
print("original list: ", food)
#using .append
food.append("sandwich")
print("appended list:", food)
#len
print("length of list:", len(food))
original list: ['noodles', 'cereal', 'bread', 'ice cream', 'chicken']
appended list: ['noodles', 'cereal', 'bread', 'ice cream', 'chicken', 'sandwich']
length of list: 6
//3.10.3 popcorn hack in js (bonus)
let favFood = ["noodles", "cereal", "bread", "ice cream", "chicken"];
console.log("original list:", favFood);
//using push
favFood.push("sandwich");
console.log("using push: ", favFood);
// len
console.log("Total number of items in the list:", favFood.length);
//3.10.4 js popcorn hack
let fruits = ["apple", "banana", "orange"];
if (fruits.includes("banana")) {
console.log("banana is in the list");
} else {
console.log("banana is not in the list");
}
#3.10.4 popcorn hack in python
fruits = ["apple", "banana", "orange"]
check = "banana"
if check in fruits:
print(f"{check} is in the list")
else:
print(f"{check} is not in the list")
banana is in the list
3.10.4 homework hack:
#part 1: in python
numbers = [10, 20, 30, 40, 50]
print("second element: ", numbers[1])
// part 2: in js
let num = [10, 20, 30, 40, 50];
console.log("second element: ", num[1]);
#part 3: python to do list (users can add, remove, view items)
todo_list = []
# Display menu once
def display_menu():
print("\nTo-Do List Menu:")
print("1. Add item")
print("2. Remove item")
print("3. View list")
print("4. Quit")
#Add item
def add_item():
item = input("Enter an item to add to your to-do list: ")
todo_list.append(item)
print(f"'{item}' has been added to your to-do list.")
#remove item
def remove_item():
item = input("Enter the item to remove: ")
if item in todo_list:
todo_list.remove(item)
print(f"'{item}' has been removed from your to-do list.")
else:
print(f"'{item}' is not in the list.")
# View list
def view_list():
if not todo_list:
print("Your to-do list is empty.")
else:
print("Your to-do list:")
for i, item in enumerate(todo_list, 1):
print(f"{i}. {item}")
display_menu()
# Keep looping until user quits
while True:
choice = input("\nChoose an option (1-4): ")
if choice == '1':
add_item()
elif choice == '2':
remove_item()
elif choice == '3':
view_list()
elif choice == '4':
print("Goodbye!")
break
else:
print("Invalid choice, please choose again.")
To-Do List Menu:
1. Add item
2. Remove item
3. View list
4. Quit
'wake up' has been added to your to-do list.
'eat breakfast' has been added to your to-do list.
'go to school' has been added to your to-do list.
'eat lunch' has been added to your to-do list.
'eat breakfast' has been removed from your to-do list.
Your to-do list:
1. wake up
2. go to school
3. eat lunch
Goodbye!
//part 4: js workout tracker
let workoutLog = [];
//original workout
function logWorkout() {
let type = prompt("Workout type:");
let duration = prompt("Duration of workout (min):");
let calories = prompt("Calories burned:");
// Add to the workout log
workoutLog.push({
type: type,
duration: parseInt(duration),
calories: parseInt(calories)
});
console.log("Workout logged successfully!");
}
// Function to view all logged workouts
function viewWorkouts() {
if (workoutLog.length === 0) {
console.log("No workouts logged yet.");
} else {
console.log("Logged Workouts:");
workoutLog.forEach((workout, index) => {
console.log(`${index + 1}. ${workout.type} - Duration: ${workout.duration} mins, Calories burned: ${workout.calories}`);
});
}
}
// Function to display the menu
function displayMenu() {
console.log("\nWorkout Tracker Menu:");
console.log("1. Log a workout");
console.log("2. View workouts");
console.log("3. Quit");
}
// Main program loop
while (true) {
displayMenu();
let choice = prompt("Choose an option (1-3):");
if (choice === '1') {
logWorkout();
} else if (choice === '2') {
viewWorkouts();
} else if (choice === '3') {
console.log("Goodbye!");
break;
} else {
console.log("Invalid choice, please try again.");
}
}