Skip to the content.

3.10 Hw_ipynb_2_

def find_max_min(numbers):
    # Initialize max and min with the first element of the list
    max_value = numbers[0]
    min_value = numbers[0]

    # Iterate through the list to find max and min
    for num in numbers:
        if num > max_value:
            max_value = num
        if num < min_value:
            min_value = num

    return max_value, min_value

# Example usage:
numbers = [34, 12, 45, 2, 67, 23, 89, 1]
max_value, min_value = find_max_min(numbers)

print(f"Maximum value: {max_value}")
print(f"Minimum value: {min_value}")
Maximum value: 89
Minimum value: 1
// Step 1: Create an array with the integers in random order
let randomNumbers = [9, 1, 80, 3, 77, 4, 8]; // These are the same integers but in random order

// Step 2: Sort the array in numerical order using the sort() method
randomNumbers.sort(function(a, b) {
    return a - b; // Numerical comparison
});

// Output the sorted array
console.log("Sorted numbers:", randomNumbers);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Shopping List</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        #shopping-list {
            margin-top: 20px;
        }
    </style>
</head>
<body>

<h1>Shopping List</h1>

<!-- Input field for item name -->
<label for="itemName">Item Name:</label>
<input type="text" id="itemName" placeholder="Enter item name">
<br>
<button onclick="addItem()">Add Item</button>
<button onclick="removeItem()">Remove Item</button>
<button onclick="viewList()">View List</button>
<button onclick="clearList()">Clear List</button>

<!-- Area to display the shopping list -->
<div id="shopping-list"></div>

<script>
    // Shopping list array
    let shoppingList = [];

    // Function to add item to the shopping list
    function addItem() {
        const itemName = document.getElementById('itemName').value;

        // Check if item name is valid
        if (itemName === '') {
            alert('Please enter a valid item name.');
            return;
        }

        // Add item to the shopping list
        shoppingList.push(itemName);

        // Clear input field
        document.getElementById('itemName').value = '';

        alert(`${itemName} has been added to the shopping list.`);
    }

    // Function to remove item from the shopping list
    function removeItem() {
        const itemName = document.getElementById('itemName').value;

        // Find the index of the item in the shopping list
        const index = shoppingList.findIndex(item => item.toLowerCase() === itemName.toLowerCase());

        // If item is found, remove it
        if (index !== -1) {
            shoppingList.splice(index, 1);
            alert(`${itemName} has been removed from the shopping list.`);
        } else {
            alert(`${itemName} is not in the shopping list.`);
        }

        // Clear input field
        document.getElementById('itemName').value = '';
    }

    // Function to view the shopping list
    function viewList() {
        const shoppingListDiv = document.getElementById('shopping-list');
        shoppingListDiv.innerHTML = ''; // Clear previous list view

        if (shoppingList.length === 0) {
            shoppingListDiv.innerHTML = '<p>Your shopping list is empty.</p>';
            return;
        }

        // Create a list to display the items
        const list = document.createElement('ul');

        shoppingList.forEach(item => {
            const listItem = document.createElement('li');
            listItem.textContent = item;
            list.appendChild(listItem);
        });

        shoppingListDiv.appendChild(list);
    }

    // Function to clear the shopping list
    function clearList() {
        shoppingList = [];
        document.getElementById('shopping-list').innerHTML = '<p>Your shopping list has been cleared.</p>';
    }
</script>

</body>
</html>