List

  • A list is like a container that can hold multiple elements. Each element is identified by its position or index.

  • List Operations:

    • aList[i]: Accesses the element of aList at index i.
      • An index is a numeric value representing the position of an element.
      • Example: The first element of aList is at index 0, represented by aList[0].
    • x <- aList[i]:
      • Assigns the value of aList[i] to variable x.
    • aList[i] <- x:
      • Assigns the value of x to aList[i].
    • aList[i] <- aList[j]:
      • Assigns the value of aList[j] to aList[i].
    • INSERT(aList, i, value):
      • Inserts value into aList at index i.
      • aList is the list, i is the index, and value is the element to insert.
    • APPEND(aList, value):
      • Adds value to the end of aList.
    • REMOVE(aList, i):
      • Removes the element at index i from aList.
    • LENGTH(aList):
      • Returns the number of elements in aList.
    • FOR EACH item IN aList { ... }:
      • Iterates over each element (item) in aList from the first to the last.
      • The code inside the loop is executed once for each item.


    Append

  • To add an element at the end of the list

  • aList  []
    
    USER_INPUT  ("Enter an item you want (or 'q' to quit): ")
    
    REPEAT UNTIL USER_INPUT  q{
        APPEND (aList, USER_INPUT)
    }
    
    DISPLAY(aList)
    

    Popcorn Hack:

    • Use a_list.append(user_input) to append each item.
    • Use a for loop to print out each item in the list at the end.

    Exercise: Build a Shopping List with JavaScript

    In this exercise, you will create a simple shopping list application that allows users to add items to a list and display them at the end.

    Instructions:

    1. Set up the array:
      • Create an empty array aList that will store the shopping list items.
    2. User input:
      • Use prompt() to ask the user to enter an item to add to the shopping list.
      • Continue prompting the user until they type 'q' to quit.
    3. Append items to the list:
      • Use the push() method to append each item the user enters into the aList array.
    4. Display the list:
      • After each item is added, print the current shopping list to the console using console.log().
    5. Final display:
      • Once the user quits by typing 'q', display the entire shopping list.
      • Use a for loop to iterate over the array and print each item to the console.

    Starter Code

    %%js
    function shoppingList() {
        // Create an empty array to store items
        let aList = [];
        let userInput;
    
        while (true) {
            userInput = prompt("Enter an item to add to the shopping list (or 'q' to quit):");
    
            if (userInput === 'q') {
                break;
            }
    
            // TODO: Use the push method to append the user's input to the array
            // aList.push(userInput);
    
            // TODO: Display the current list after each append operation
            // console.log("Current shopping list: ", aList);
        }
    
        // Final display of the shopping list
        console.log("Your final shopping list is:");
        // TODO: Use a for loop to display all items in the list
        // for (let i = 0; i < aList.length; i++) {
        //     console.log(aList[i]);
        // }
    }
    
    shoppingList();
    

    Python Example:

    def shopping_list():
        # Create an empty list to store items
        a_list = []
    
        while True:
            user_input = input("Enter an item you want to add to the shopping list (or 'q' to quit): ")
    
            if user_input == 'q':
                break
    
            # TODO: Use the append operation to add the user input to the list
    
            # TODO: Print the current list after each append operation
        
        # Final display of the shopping list
        print("Your final shopping list is:")
        # TODO: Use a loop to display all the items in the list
    
    shopping_list()
    
    

    Java Example:

    %%js 
    // JavaScript Example: List Operations and Append
    
    function shoppingList() {
        // Create an empty array to store items
        let aList = [];
        let userInput;
    
        while (true) {
            userInput = prompt("Enter an item to add to the shopping list (or 'q' to quit):");
    
            if (userInput === 'q') {
                break;
            }
    
            // TODO: Use the push method to append the user's input to the array
            // aList.push(userInput);
    
            // TODO: Display the current list after each append operation
            // console.log("Current shopping list: ", aList);
        }
    
        // Final display of the shopping list
        console.log("Your final shopping list is:");
        // TODO: Use a for loop to display all items in the list
        // for (let i = 0; i < aList.length; i++) {
        //     console.log(aList[i]);
        // }
    }
    
    shoppingList();