<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
%%js
//POPCORN HACK 3
//array of numbers
let numbers = [10, 15, 20, 25, 30, 35, 40];
//Use filter() to filter only even numbers
let even_numbers = numbers.filter(number => number % 2 === 0);
// Print the filtered array (list of even numbers)
console.log("Filtered array with even numbers:", even_numbers);
<IPython.core.display.Javascript object>
#POPCORN HACK 1
# Create a list of strings/fruits
fruits = ["Apple", "Banana", "Mango"]
# Use the insert() method with negative indexes to add values
fruits.insert(-1, "Orange") # Insert at the second-to-last position
fruits.insert(-2, "Grapes") # Insert at the third-to-last position
# Print updated list
print("Updated list:", fruits)
Updated list: ['Apple', 'Banana', 'Grapes', 'Orange', 'Mango']
Combined list: [1, 2, 3, 4, 5, 6]
#POPCORN HACK 3
# Create a list of items
my_list = ["Apple", "Banana", "Cherry", "Date", "Fig", "pinapple"]
# Method 1: Using remove()
my_list.remove("Banana")
# Method 2: Using pop()
my_list.pop(2)
# Method 3: Using del
del my_list[0]
# Print the updated list
print("Updated list:", my_list)
Updated list: ['Cherry', 'Elderberry', 'Fig']
Current Grocery List:
['apple', 'cereal', 'banana']
Sorted Grocery List:
['apple', 'banana', 'cereal']
Updated Grocery List after removal:
['banana', 'cereal']
Original List:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
List of Even Numbers:
[2, 4, 6, 8, 10, 12, 14, 16, 18]
All grades: [50, 70, 99]
Passing Grades: [70, 99]
#HOMEWORK 4
#Create a list of numbers from 1 to 10
numbers = list(range(1, 11))
#Print original list
print("Original list:", numbers)
#Sort the list highest to lowest
numbers.sort(reverse=True)
print("List in descending order:", numbers)
#Slice the list to the first five numbers and print
first_five = numbers[:5]
print("First five numbers:", first_five)
#Sort the list from lowest to highest
numbers.sort()
print("List in ascending order:", numbers)
Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
List in descending order: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
First five numbers: [10, 9, 8, 7, 6]
List in ascending order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
%%js
//HOMEWORK 1
//Create an array of 5 values
let myArray = [10, 20, 30, 40, 50];
//Display original array
console.log("Original array:", myArray);
//Reverse the array with reverse()
myArray.reverse();
//Display reversed array
console.log("Reversed array:", myArray);
%%js
//HOMEWORK 3
//Initialize the array of four items
let choresList = ["dishes", "laundry", "vacuum", "groceries"];
//Display original list
console.log("Original list:", choresList);
//Use push() to add item to list
choresList.push("take out trash");
console.log("After push (add 'take out trash'):", choresList);
//Use shift() to remove first item
choresList.shift();
console.log("After shift (remove first item):", choresList);
//Use pop() to remove the last item
choresList.pop();
console.log("After pop (remove last item):", choresList);
//Use unshift() to add a item to the beginning of the array
choresList.unshift("make bed");
console.log("After unshift (add 'make bed'):", choresList);
//Use push() to add multiple values at once
choresList.push(...["clean windows", "mop floor", "dust furniture"]);
console.log("After using push() and spread operator to add multiple items:", choresList);
%%js
//HOMEWORK 4
//Create array with ten numbers
let randomNumbers = [12, 5, 8, 21, 34, 7, 18, 13, 26, 9];
//Function to count how many even numbers are in the array
function countEvenNumbers(arr) {
let evenCount = 0;
// Iterate through the array and count even numbers
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 0) {
evenCount++;
}
}
//Return the count of even numbers
return evenCount;
}
//Call the function and display the result
let evenNumberCount = countEvenNumbers(randomNumbers);
console.log("Count of even numbers:", evenNumberCount);
<IPython.core.display.Javascript object>