# Step 1: Create an empty list to store grocery items
grocery_list = []
# Step 2: Input three grocery items and add them to the list
for _ in range(3):
item = input("Enter a grocery item: ")
grocery_list.append(item)
# Step 3: Display the current grocery list after all items are added
print("Current grocery list:", grocery_list)
# Step 4: Sort the list alphabetically and print the sorted list
grocery_list.sort()
print("Sorted grocery list:", grocery_list)
# Step 5: Remove one item specified by the user and display the updated list
item_to_remove = input("Enter an item to remove from the list: ")
grocery_list.remove(item_to_remove)
print("Updated grocery list:", grocery_list)
Problem #2 - Filtering Even Numbers
Objective:
Create a list of numbers and filter for even numbers.
# Step 1: Create a list of integers from 1 to 20
numbers = list(range(1, 21))
# Step 2: Print the original list
print("Original list of numbers:", numbers)
# Step 3: Create a new list that contains only the even numbers using list comprehension
even_numbers = [num for num in numbers if num % 2 == 0]
# Step 4: Print the list of even numbers
print("List of even numbers:", even_numbers)
Problem #3 - Student Grades Manager
Objective:
Write a program that manages a list of student grades.
# Step 1: Create an empty list to store student grades
grades = []
# Step 2: Input three grades (as integers) and add them to the list
for _ in range(3):
grade = int(input("Enter a student grade: "))
grades.append(grade)
# Step 3: Print the list of grades after all grades are entered
print("List of grades:", grades)
# Step 4: Create a new list that contains only grades above 60 and print this list
passing_grades = [grade for grade in grades if grade > 60]
print("Grades above 60:", passing_grades)
Problem #4 - Number List Operations
Objective:
Create a list of numbers and perform basic operations.
# Step 1: Create a list of numbers from 1 to 10
numbers = list(range(1, 11))
# Step 2: Print the original list
print("Original list:", numbers)
# Step 3: Sort the list in descending order
numbers.sort(reverse=True)
print("List in descending order:", numbers)
# Step 4: Slice the list to get the first five numbers and print them
first_five = numbers[:5]
print("First five numbers:", first_five)
# Step 5: Sort the list again in ascending order and print it
numbers.sort()
print("List in ascending order:", numbers)
JavaScript Homework
Problem #1: Basic Array Creation
Objective:
- Create an array in JavaScript with at least 5 values.
- Use
console.log()
to display the array.
Bonus:
- Use the
reverse()
popcorn hack to reverse your array.
%%js
// Step 1: Create an array with at least 5 values
let myArray = [10, 20, 30, 40, 50];
// Step 2: Display the array
console.log("Original array:", myArray);
// Bonus: Use the reverse() hack to reverse the array
myArray.reverse();
console.log("Reversed array:", myArray);
Problem #2: Accessing Elements
Objective:
- Given the array
sports = ["soccer", "football", "basketball", "wrestling", "swimming"]
, display the values “soccer” and “wrestling” using their indexes.
%%js
// Step 1: Create the sports array
let sports = ["soccer", "football", "basketball", "wrestling", "swimming"];
// Step 2: Access and display the elements "soccer" and "wrestling"
console.log("First sport:", sports[0]); // soccer
console.log("Fourth sport:", sports[3]); // wrestling
Problem #3: Adding and Removing Items
Objective:
- Create an array called
choresList
initialized with four items of your choice. - Use
push()
,shift()
,pop()
, andunshift()
to change the list. - Use
console.log()
to display the output each time the list changes.
Bonus:
- Use the
push()
and spread operator popcorn hack to add multiple values to yourchoresList
at once.
%%js
// Step 1: Create the choresList array with 4 items
let choresList = ["dishes", "laundry", "vacuuming", "dusting"];
// Step 2: Add an item using push()
choresList.push("groceries");
console.log("After push:", choresList);
// Step 3: Remove the first item using shift()
choresList.shift();
console.log("After shift:", choresList);
// Step 4: Remove the last item using pop()
choresList.pop();
console.log("After pop:", choresList);
// Step 5: Add an item to the beginning using unshift()
choresList.unshift("cooking");
console.log("After unshift:", choresList);
// Bonus: Use push() and spread operator to add multiple items
choresList.push(...["trash", "mopping"]);
console.log("After adding multiple items with spread operator:", choresList);
Problem #4: Iteration and Conditionals
Objective:
- Create an array that contains ten random numbers (both even and odd).
- Write a function that iterates through the array and counts how many even numbers appear.
- Return the count and display the output using
console.log()
.
%%js
// Step 1: Create an array with 10 random numbers
let randomNumbers = [3, 4, 7, 12, 19, 22, 25, 30, 33, 40];
// Step 2: Function to count even numbers
function countEvenNumbers(arr) {
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
count++;
}
}
return count;
}
// Step 3: Display the count of even numbers
console.log("Number of even numbers:", countEvenNumbers(randomNumbers));