1/70
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
Which of the following statements will change “tennis” to “gaming” ?:
var hobbies = [“tennis”, “reading”, “football”, “hiking”];
hobbies[1] = “gaming”;
hobbies[“tennis”] = “gaming”;
hobbies.pop(“tennis”);
hobbies[0] = “gaming”;
Indexing starts at one!
1
How to print out index 1?
// A variable can hold a whole list or array instead of just one value!
var arr = [5, 8, 103, 32, 7];
println(arr[0]);
println(arr[2]);
var shoppingList = ["bread", "eggs",
"milk", "cookies"];
// Let's print out index 1
// Let's get cake instead of milk
println(shoppingList[2]);
shoppingList[2] = "cake";
println(shoppingList[2]);
}
println(shopping list[1]);
Create an array of the top 5 places you would like to travel called travelList. Print out the element at index 2.
The element is the individual value in a list that is assigned a unique index.
The index is the method for referencing the elements in a list. Rememeber that the index starts at zero!
function start(){
//Make travle list variable
var travelList = ["Florida" , "The Bahamas", "Ireland", "England", "Paris"];
//Print out element at index two
println(travelList[2]);
}
Create an array (the list must have the name numList) of the first 5 prime numbers starting at 2. Print out the items at index 0, 2, and 4.
function start(){
//Num list array
var numList = [2, 3, 5, 7, 11];
//Print out items at index 0, 2, 4
println(numList[0]);
println(numList[2]);
println(numList[4]);
}
Consider the following code segment.
aList ← [15, "hello", true] Which element can be found at index 2?
hello
Which of the following will create an empty list and assign it to aList?
aList ← 0
aList ← [0]
aList ← [ ]
aList ← " "
3
Consider the following code segment.
aList ← [20, 40, 60]
bList ← [10, 30, 50]
aList ← bList
bList ← [15, 45, 90]What is stored in aList?
[15, 45, 90]
[10, 30, 50]
[20, 40, 60]
[ ] (empty set)
2
The indices on the exam are different. They do not start at 0, but rather at 1.
aList ← ["dog", false, "bird"] Which of the following will throw an error?
I. aList[0]
II. aList[2]
III. aList[3]
IV. aList[4]
I and II
I and IV
II and IV
III and IV
2
What is an array (or list)?
An ordered collection of items
An unordered collection of items
A function
A type of loop
1
We want to make a grocery list in our program. Which of the following is the correct way to make an array/list?
1.
var groceries = "milk, sugar, eggs, cake";JavaScript
2.
var groceries = ["milk, sugar, eggs, cake"];JavaScript
3.
var groceries = ["milk" "sugar" "eggs" "cake"];JavaScript
4.
var groceries = ["milk", "sugar", "eggs", "cake"];JavaScript
4
What is the index of the very first item in a JavaScript array?
0
1
1 —> 0
var shoppingList = ["milk", "eggs", "sugar", "bread", "cake"];JavaScript
Which line of code will correctly change "cake" to "apples"?
1.
shoppingList["cake"] = "apples";JavaScript
2.
shoppingList["apples"] = "cake";JavaScript
3.
shoppingList[4] = "apples";JavaScript
4.
shoppingList[5] = "apples";JavaScript
3.
What would go in the bold?
function start(){
var shoppingList = ["bread", "eggs", "milk", "cookies"];
println(shoppingList[0]);
println(shoppingList[1]);
println(shoppingList[2]);
println(shoppingList[3]);
shoppingList[2] = "tuna";
println(shoppingList[2]);
var idx = 0;
//What would go here?
var val = shoppingList[1];
println(val);
}
println(shoppingList[idx]);
Create an array of your favorite 4 movies. Print out the 0th element in the array. Now set the 0th element to be “Star Wars” and try printing it out again.
function start(){
// Favorite movies
var favMovies = ["Cruella", "The Secret Garden", "Home Alone",
"A Christmas Carol"];
//Print fav movie 0
println(favMovies[0]);
//Set 0 element to be Star Wars
favMovies[0] = "Star Wars";
println(favMovies[0]);
}
Answer the questions:
function start(){
var arr = [];
arr.push(5);
println(arr[0]);
println(arr[1]);
//Push does what?
arr.push(12);
arr.push(1);
arr.push(1000);
arr.push(3);
println(arr[4]);
//Pop does what?
var last = arr.pop();
println(last);
println(arr[4]);
}
Push lets you add to the array, ppop removes the last element
Lists and arrays are an example of a data abstraction.
A data abstraction can often contain different types of elements. It also provides a separation between the abstract properties of a data type (integer, boolean, string) and the concrete details of its representation. Basically, it doesn’t matter that the types are different!
Create an empty array. Push the integer 3, a string of “hello” and a boolean of false into the array. Print out each index individually. Now, pop the last two elements.
Now, print out the first three values again.
function start(){
//Create empty array
var emptyArr = [];
//Push integer 3, "hello," false
emptyArr.push(3);
emptyArr.push("hello");
emptyArr.push(false);
//Print out each index
println(emptyArr[0]);
println(emptyArr[1]);
println(emptyArr[2]);
//Pop last two elements
emptyArr.pop();
emptyArr.pop();
//Print first three values
println(emptyArr[0]);
println(emptyArr[1]);
println(emptyArr[2]);
}
What index refers to the “end” of an array?
The lowest index, index 0
The highest index
2
var numbers = [0, 1, 2, 3, 4];JavaScript
How can we add a 5 to the end of numbers?
numbers[5] = 5;numbers[0] = 5numbers.push(5);numbers.pop(5);3.
var numbers = [0, 1, 2, 3, 4, 5];JavaScript
How can we remove the last item from the end of the numbers array and store it in a variable called value?
var value = numbers[5];var value = numbers.pop();var value = numbers.push();var value = numbers;2.
How can we get the length of an array arr?
arr.len
arr.length
length(arr)
arr.length()
2.
Which loop will correctly loop over the array arr and print each value inside of it?
for(var i = 0; i < arr.length(); i++){
var curr = arr[i];
println(curr);
}for(var i = 1; i <= arr.length(); i++){
var curr = arr[i];
println(curr);
}for(var i = 0; i < arr.length; i++){
var curr = arr[i];
println(curr);
}for(var i = 1; i <= arr.length; i++){
var curr = arr[i];
println(curr);
}2.
function start(){
var arr = ["bread", "eggs",
"milk", "cookies"];
//What goes here?
var cur = arr[i];
println(cur);
}
var list = ["bread", "eggs", "milk", "cookies", "bananas", "tuna", "lettuce", "yogurt", "cheese", "chicken", "cucumbers", "orange juice", "salt", "doritos", "lemonade", "apples", "paper towels", "red onion", "minced garlic", "tumeric", "donuts", "bagels", "crackers", "red pepper", "green pepper", "spinach", "canola oil", "vanilla", "flour", "brown sugar", "powdered sugar", "lime"];
for(var i = 0; i < list.length; i++){
println(list[i]);
}
}
for(var i = 0; i < arr.length; i++){
Traverse through a loop with the list ["bread", "eggs", "milk", "cookies", "bananas", "tuna", "lettuce", "yogurt", "cheese", "chicken", "cucumbers", "orange juice", 'lime, potatoes']; and print the first five items in that list
function start(){
var list = ["bread", "eggs", "milk", "cookies", "bananas", "tuna", "lettuce", "yogurt", "cheese", "chicken", "cucumbers", "orange juice", 'lime, potatoes'];
// Prints the first 5 items in the list
for(var i = 0; i < 5; i++){
println(list[i]);
}
// Prints the first half of the list
for(var i = 0; i < list.length/2; i++){
println(list[i]);
}
}
function start(){
var arr = [1, 8, 123, 103, 992,
21, 2, 2, 144, 523,
13, 90, 77, 12, 13];
var sum = 0;
for(var i = 0; i < arr.length; i++){
var cur = arr[i];
//What goes here?
}
println(sum);
}
sum += cur;
// This code segment traverses through the array and prints the maximum
// value.
// Challenge: Can you change this code so that the min value is printed?
function start(){
var arr = [1, 8, 123, 103, 992,
21, 2, 2, 144, 523,
13, 90, 77, 12, 13];
var max = arr[0]
for(var i = 0; i < arr.length; i++){
if (arr[i] > max){
max = arr[i];
}
}
println(max);
}
Write a program that outputs the product (the result of multiplying together) all of the elements in an array.
Create an array arr with the values [1, 8, 3, 4, 2, 9].
The answer should be 1728.
function start(){
//Arr
var arr = [1, 8, 3, 4, 2, 9];
//Set total = 1
var total = 1;
//Multiply them
for(var i = 0; i < arr.length; i++){
var cur = arr[i];
total *= cur;
}
println(total);
}
Write a program that prints out a flight itinerary. A flight itinerary is a list
of cities, and should be output like this.
For example, if the flights array is
["San Francisco", "New York", "Chicago", "Honolulu"]You print
San Francisco -> New York -> Chicago -> HonoluluHelpful Hint: To print multiple times on the same line, use print() instead of println(). For example, this code:
print("hello, ");
print("world");prints each statement on the same line, giving the output of:
hello, worldfunction start(){
//Prints out flight itinerary
//Array of flights
var arr = ["San Francisco", " New York", " Chicago", " and Honolulu"];
//Print itinerary
print("Hello, ");
print("world.");
print(" Our flight itinerary is " + arr);
}
Write a function called
function onlyEvens(arr)That takes an array and returns an array with only the even numbers in the original array.
Then, you should print out the new list.
Consider the following code snippet:
var arr = [1,2,3,4,5,6];
var evens = onlyEvens(arr);
println(evens);Depending on the environment you are running your code in (a setting you can change in your editor), your output for the code above can look like this:
2,4,6Or this:
[2,4,6]Both outputs are acceptable for this exercise.
//Var evens
var evens = [];
//Var arr
var arr = [1, 2, 3, 4, 5, 6,];
function start(){
onlyEvens(arr);
printEvens(evens);
}
//Function
function onlyEvens(arr){
//Gets arr whole length
for(var i = 0; i < arr.length; i++){
//If arr even/odd statement
if(arr[i] % 2 == 0){
evens.push(arr[i]);
}
}
}
function printEvens (evens){
for(var i = 0; i < evens.length; i++){
println(evens[i]);
}
}
What if arrays did not exist?!
Using the variables below, write a function named oddOrEven that checks if each variable is an even or odd number and prints out the result.
var num1 = 15;
var num2 = 32;
var num3 = 19;
var num4 = 11;
var num5 = 28;
var num6 = 24;function start(){
var num1 = 15;
var num2 = 32;
var num3 = 19;
var num4 = 11;
var num5 = 28;
var num6 = 24;
oddOrEven(num1);
oddOrEven(num2);
oddOrEven(num3);
oddOrEven(num4);
oddOrEven(num5);
oddOrEven(num6);
}
function oddOrEven (num){
if(num % 2 == 0){
println("even");
}else{
println("odd");
}
}
function start(){
//this array is filled with several Rectangle objects
var rectangleArray = [...];
//your code here
...
}
function updateRectangle(rect){
//changes the color of rect to a random color
rect.setColor(Randomizer.nextColor());
}JavaScript
We have an array of Rectangles called rectangleArray and a function updateRectangle(rect) that takes a single rectangle as a parameter and changes its color to a random color. How can we easily change the color of every rectangle inside rectangleArray?
hange the color of every rectangle inside rectangleArray?
updateRectangle(rectangleArray[0]);
updateRectangle(rectangleArray[1]);
...
updateRectangle(rectangleArray[100]);
...
updateRectangle(rectangleArray[rectangleArray.length - 1]);updateRectangle(rectangleArray);for(var i = 0; i < rectangleArray.length; i++){
updateRectangle(rectangleArray[i]);
}3
What kinds of items can we store in arrays?
numbers only
strings of text only
numbers or strings of text
any kind of object (anything you can store in a variable)
4.
*/
function start(){
var flips = flipCoins();
//What goes here?
}
// This function should flip a coin NUM_FLIPS
// times, and add the result to an array. We
// return the result to the caller.
function flipCoins(){
var flips = [];
for(var i = 0; i < NUM_FLIPS; i++){
if(Randomizer.nextBoolean()){
//What goes here?
}else{
flips.push("Tails");
}
}
return flips;
}
function printArray(arr){
for(var i = 0; i < arr.length; i++){
//What goes here?
}
}
printArray(flips);, flips.push("Heads");, println(i + ": " + arr[i]);
var RADIUS = 40;
var NUM_CIRCLES = 7;
// FLASH WARNING: Photosensitive individuals should not set DELAY to a value
// below 300. (i.e. rates that flash greater than three frames per second)
var DELAY = 350;
var circles = [];
function start(){
createCircles();
setTimer(goCrazy, DELAY);
}
function createCircles(){
for(var i = 0; i < NUM_CIRCLES; i++){
//What goes here?
updateCircle(circle);
add(circle);
circles.push(circle);
}
}
function goCrazy(){
for(var i = 0; i < circles.length; i++){
updateCircle(circles[i]);
}
}
function updateCircle(circle){
var x = Randomizer.nextInt(
RADIUS, getWidth() - RADIUS);
var y = Randomizer.nextInt(
RADIUS, getHeight() - RADIUS);
//What goes here?
circle.setColor(Randomizer.nextColor());
}
var circle = new Circle(RADIUS);, circle.setPosition(x, y);
Starting with our program that creates an array of coin flips, add a function that prints out the total number of heads and total number of tails flipped.
Your function should take the array of flips as a parameter like this:
function countHeadsAndTails(flips) {
// your code here
}var NUM_FLIPS = 100;
function start(){
var flips = flipCoins();
printArray(flips);
}
// This function should flip a coin NUM_FLIPS
// times, and add the result to an array. We
// return the result to the caller.
function flipCoins(){
var flips = [];
for(var i = 0; i < NUM_FLIPS; i++){
if(Randomizer.nextBoolean()){
flips.push("Heads");
}else{
flips.push("Tails");
}
}
return flips;
}
function printArray(arr){
for(var i = 0; i < arr.length; i++){
println(i + ": " + arr[i]);
}
}
Add at the end:
function countHeadsAndTails(flips){
var headsTotal = 0;
var tailsTotal = 0;
for(var i = 0; i < NUM_FLIPS; i++){
if(flips[i] == "Heads"){
headsTotal += 1;
}
if(flips[i] == "Tails"){
tailsTotal += 1;
}
}
println("The total heads is " + headsTotal);
println("The total tails is " + tailsTotal);
}
var groceries = ["milk", "eggs", "bread", "cheese", "yogurt", "sugar"];JavaScript
What will be the result of groceries.indexOf("yogurt"); ?
true
false
4
5
3 → 4
var groceries = ["milk", "eggs", "bread", "cheese", "yogurt", "sugar"];JavaScript
What will be the result of groceries.indexOf("cake"); ?
true
false
undefined
-1
4.
What method can we use to find the index of a particular element in an array?
indexOf
find
findElement
index
1.
function indexOf(arr, str){
for(var i = 0; i < arr.length; i++){
var cur = arr[i];
if(cur == str){
return i;
}
}
return -1;
}
function start(){
var list = ["bread", "eggs", "milk", "cookies", "bananas", "tuna", "lettuce", "yogurt", "cheese", "chicken", "cucumbers", "orange juice", "salt", "doritos", "lemonade", "apples", "paper towels", "red onion", "minced garlic", "tumeric", "donuts", "bagels", "crackers", "red pepper", "green pepper", "spinach", "canola oil", "vanilla", "flour", "brown sugar", "powdered sugar", "lime"];
var idx = indexOf(list, "donuts");
//What goes here?
println(indexOf(list, "apple juice"));
println(list.indexOf("donuts"));
println(list.indexOf("apple juice"));
}
println(idx);
You are given an array of names of people who are in line. Try using if statements and the indexOf
method of arrays to see if Bob is in line.
var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];
var line2 = ["Tony", "Lisa", "Laurie", "Karen"];You should print whether Bob is in each line. Your console should look something like this:
Bob is in the first line.
Bob is not in the second line.function start(){
var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];
var line2 = ["Tony", "Lisa", "Laurie", "Karen"];
var one = line.indexOf("Bob");
var two = line2.indexOf("Bob");
if(one != -1){
println("Bob is in the first line.");
}else{
println("Bob is not in the first line.");
}
if(two != -1){
println("Bob is in the second line.");
}else{
println("Bob is not in the second line.");
}
}
What is the output of the following program?
function start(){
var arr = [1, 2, 3, 4, 5, 6, 7];
var elem = arr.pop();
println(arr);
println(elem);
}[1, 2, 3, 4, 5, 6]
[7][1, 2, 3, 4, 5, 6]
7[2, 3, 4, 5, 6, 7]
1[2, 3, 4, 5, 6, 7]
[1]2.
What is the output of the following program?
function start(){
var arr = [1, 2, 3, 4, 5, 6, 7];
var elem = arr.remove(2);
println(arr);
}1.
[1, 2, 3, 4, 5]2.
[3, 4, 5, 6, 7]3.
[1, 3, 4, 5, 6, 7]4.
[1, 2, 4, 5, 6, 7]4.
function start(){
var arr = [7, 4, 14, 8, 3, 9];
println(arr);
arr.pop();
println(arr);
//What goes here?
//arr.splice(2, 1);
println(arr);
println("We removed: " + elem);
}
var elem = arr.remove(2);
You are given an array of names of people who are in line for movie tickets.
var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan"];Write a function that prints the names in the list before removing the first two names. Then, use the .remove() method to remove the first person from the line twice, as if you have just given them their tickets. After, print the names that remain in the list.
The console should print this:
Sam, Lisa, Laurie, Bob, Ryan
Laurie, Bob, Ryanfunction start(){
var line = ["Sam", "Lisa", "Laurie", "Bob", "Ryan."];
println(line);
line.remove(0);
line.remove(0);
println(line);
}
The AP Exam uses the following format to iterate through a list.
FOR EACH item IN aList
{
<block of statements>
}Given the following code segment, how can you best describe its behavior?
result ← 0
FOR EACH n IN list
{
IF (n MOD 2 = 0)
{
result ← result + n
}
}
DISPLAY resultThis code displays the count of even numbers in list
This code displays the sum of the even numbers in list
This code displays the sum of the numbers at the even indices of list
This code results in an error
2.
The AP Exam uses the following format to add an item to list and to find the length of a list.
APPEND(aList, value)
LENGTH(aList)Given the following code segment, how can you best describe its behavior?
You can assume number is a positive integer and isPrime(x) returns true if x is prime and false otherwise.
list ← []
i ← 1
REPEAT number TIMES
{
IF (isPrime(i))
{
APPEND(list, i)
}
i ← i + 1
}
DISPLAY LENGTH(list)This code displays the total number of prime numbers between 1 and number
This code displays a list of all of the prime numbers between 1 and number
This code displays the sum of the prime numbers between 1 and number
This code results in an error
1.
Given the following Mystery procedure, how can you best describe the result it returns?
PROCEDURE Mystery (list, target)
{
length ← LENGTH(list)
i ← 1
REPEAT length TIMES
{
IF (list[i] = target)
{
REMOVE(list, i)
}
ELSE
{
i ← i + 1
}
}
RETURN(list)
}This procedure returns the list without the first instance of the given target
This procedure returns the list without any instance of the given target
This procedure returns the list where each instance of the given target is replaced by the index.
This procedure returns the number of times the target appears in the list
2.
The AP Exam uses the following format to insert an item to a list and to generate a random integer from a to b, including a and b.
INSERT(aList, i, value)
RANDOM(a, b)Given the following code segment, how can you best describe its behavior?
i ← 1
FOR EACH x IN list
{
REMOVE(list, i)
random ← RANDOM(1, LENGTH(list))
INSERT(list, random, x)
i ← i + 1
}This code replaces everything in the list with random numbers.
This code shuffles the order of the numbers in the list by removing them and inserting them back in a random place.
This code removes all of the numbers in the list and inserts random numbers in random places in the list.
This code errors by trying to access an element at an index greater than the length of the list.
2.
Write a program that simulates a coin flipping. Each time the program runs, it should print either “Heads” or “Tails”.
There should be a 0.5 probability that “Heads” is printed, and a 0.5 probability that “Tails” is printed.
There is no actual coin being flipped inside of the computer, and there is no simulation of a metal coin actually flipping through space. Instead, we are using a simplified model of the situation, we simply generate a random probability, with a 50% chance of being true, and a 50% chance of being false.
function start() {
if(Randomizer.nextInt(1, 2) == 1){
println("heads");
}else{
println("tails");
}
}
How to index or get “candy” in the array: var shoppingList = [“food”, “candy”, “bread”, “water”];
shoppingList[1]
How to make an index value a variable?
//Turns into diff value
var idx = 2;
list[idx] = “tuna”;
How to code an empty array and add to it and remove the end.
varr arr = [];
arr.push(5);
arr.push(12);
arr.push(3);
var last = arr.pop();
println(arr);
println(last);
//5, 12 will pint then 3 will print
Print out shopping list when looping through arrays.
for(var I = 0; i < list.length; i++){
var curr = arr[i];
println(curr);
}
Simulations mimic real-world events without what?
The cost or danger or building and testing
Simulations are generated based on a ______ of how the real world actually works.
model
Model
A set of rules for how things interact in a simulation
The ______ the model, the faster the simulation.
simpler
Ignore _____, gravity, or having ___ dimensions depending on what you are simulating.
disease, 2
Simulations are good at ________, and they help us test our ______.
predicting, hypotheses
Write a program that simulates a coin flipping. Each time the program runs, it should print either “Heads” or “Tails”.
There should be a 0.5 probability that “Heads” is printed, and a 0.5 probability that “Tails” is printed.
There is no actual coin being flipped inside of the computer, and there is no simulation of a metal coin actually flipping through space. Instead, we are using a simplified model of the situation, we simply generate a random probability, with a 50% chance of being true, and a 50% chance of being false.
function start() {
if(Randomizer.nextInt(1, 2) == 1){
println("heads");
}else{
println("tails");
}
}
List
Also called an array. A data structure that holds a collection of values in a particular order
Push
To add an item to a list or array
Pop
To remove the item in the last position from an array
Data Structure
A particular way of organizing data in our programs.
Array Index
The position of an element in an array. The first element is at index 0, the second element is at index 1, and so on.
Indexing into an array
Getting a value at a particular index in an array.
array.length
Returns the length of the array
array[index]
Accesses an element in the array to either update or retrieve.
Simulation
An animated model that represents a real-life thing, process, or situation.
remove(index)
This function removes an element from the given index position.
Array
Also called a list. A data structure that holds a collection of values in a particular order