Fall 2024 - P4
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.10.4 Minimum Value and Finding an Element
Finding the Minimum value of a list Javascript
%%js
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let min_value = nums[0]; // Start by assuming the first number is the minimum
for (let score of nums) {
if (score < min_value) {
min_value = score; // Update min_value if a smaller number is found
}
}
console.log("Minimum value in the list:", min_value);
Finding the Minimum Value of a List in Python
nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
min_value = nums[0] # Start by assuming the first number is the minimum
for num in nums:
if num < min_value:
min_value = num # Update min_value if a smaller number is found
print("Minimum value in the list:", min_value)
Popcorn Hack: Find the sum of all the even numbers of a list called nums with integers using the previous list opperations.Define your list and define a variable to represent a potential value.
Hack: Define your list and define a variable to represent a potential value.
In JavaScript, you can check if an element exists in an array. The includes method returns true or false
%%js
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let elementToCheck = 5;
if (nums.includes(elementToCheck)) {
console.log(`${elementToCheck} exists in the array`);
} else {
console.log(`${elementToCheck} does not exist in the array`);
}
You can also use If Else statements, like the following Python example.
my_list = [10, 20, 30, 40, 50]
# Check for an element
element_to_check = 30
if element_to_check in my_list:
print(f"{element_to_check} is in the list.")
else:
print(f"{element_to_check} is not in the list.")
Popcorn Hack
%%js
let fruits = ["apple", "banana", "orange"];
Popcorn Hack: Look for the element “banana” in the list “fruits” using If Else Statements
Hack: Use if else statements for an efficient way to determine the presenence of a certain element.
Homework Hacks
- Write a Python program that creates a list of the following numbers: 10, 20, 30, 40, 50. Then, print the second element in the list.
- Write a JavaScript program that creates an array of the following numbers: 10, 20, 30, 40, 50. Then, log the second element in the array to the console.
- Python: Create a to-do list in whcih users can add, remove, and view items in their list.
- JavaScript: Create a workout tracker where users can log their workouts, including type, duration, and calories burned.