Fall 2024 - P4
| Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.10.1 List Operations and Append
List
List Operations:
aList[i]: Accesses the element ofaListat indexi.- An index is a numeric value representing the position of an element.
- Example: The first element of
aListis at index0, represented byaList[0].
x <- aList[i]:- Assigns the value of
aList[i]to variablex.
- Assigns the value of
aList[i] <- x:- Assigns the value of
xtoaList[i].
- Assigns the value of
aList[i] <- aList[j]:- Assigns the value of
aList[j]toaList[i].
- Assigns the value of
INSERT(aList, i, value):- Inserts
valueintoaListat indexi. aListis the list,iis the index, andvalueis the element to insert.
- Inserts
APPEND(aList, value):- Adds
valueto the end ofaList.
- Adds
REMOVE(aList, i):- Removes the element at index
ifromaList.
- Removes the element at index
LENGTH(aList):- Returns the number of elements in
aList.
- Returns the number of elements in
FOR EACH item IN aList { ... }:- Iterates over each element (
item) inaListfrom the first to the last. - The code inside the loop is executed once for each
item.
- Iterates over each element (
Append
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
forloop 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:
- Set up the array:
- Create an empty array
aListthat will store the shopping list items.
- Create an empty array
- 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.
- Use
- Append items to the list:
- Use the
push()method to append each item the user enters into theaListarray.
- Use the
- Display the list:
- After each item is added, print the current shopping list to the console using
console.log().
- After each item is added, print the current shopping list to the console using
- Final display:
- Once the user quits by typing
'q', display the entire shopping list. - Use a
forloop to iterate over the array and print each item to the console.
- Once the user quits by typing
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();