Editing
Other 2
Removing
Adding
Other
100

In the following array:

var groceries = ["milk", "eggs", "cookies", "cake"];

How can we change “cookies” to “bread”?

groceries[2] = "bread";

100

How can we get the length of an array arr?

arr.length

100

var numbers = [1, 1, 2, 3, 5];

How can we remove the last number?

numbers.pop()

100

var numbers = [1, 1, 2, 3, 5];

How can we add an 8 to the end of numbers?

numbers.push(8);

100

What is an array (or list)?

An ordered collection of items

200

Suppose we have an array like:

var arr = [-5, -10, 10, 5]

write the code to print only the negative numbers.

for (let i = 0; i < arr.length; i++) {

    if (arr[i] < 0) {

        console.log(arr[i]);

    }

}

200

What is the output of the following program:

var groceries = ["bread", "bananas", "apples", "cheese", "peanuts"];
println(groceries.indexOf("plums"));

-1

200

What is the output of the following program?

var arr = [1, 2, 3, 4, 5];
var removed = arr.remove(2);
println(arr);
println(removed);

[1, 2, 4, 5]
3

200

How can we print the last item of an array called arr, regardless of length.

print(arr[arr.length-1])

200

What method can we use to find the index of a particular element in an array?

indexOf

300

Write a function updateItem that takes an array, an index, and a new value as parameters. The function should update the item at the given index with the new value and return the updated array.

function updateItem(arr, index, newValue) {

    arr[index] = newValue;

    return arr; 

}

300

print out all the numbers in the list in order

var arr = [10, 20, 30, 40, 50];

for (var i = 0; i < arr.length; i++) {

    println(arr[i]);

}

300

var numbers = [1, 1, 2, 3, 5];

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.pop();

300

What does the following function mystery do?

function mystery(list){
    var result = [];
    while(list.length > 0){
        var elem = list.pop();
        result.push(elem);
    }
    return result;
}

Returns a new list containing the elements of the list list in reverse order, and leaves list empty

300

What kinds of elements can you store in data structures?

Any kind of objects (anything you can store in a variable)

400

Write a function curveGrades that takes an array of grades as a parameter. If a grade is below 50, increase it by 10. The function should return the updated array.

function curveGrades(grades) {

    for (let i = 0; i < grades.length; i++) {

        if (grades[i] < 50) {

            grades[i] += 10;

        }

    }

    return grades;

}

400

print out all the numbers in the list in reverse order

var arr = [10, 20, 30, 40, 50];

for (var i = arr.length - 1; i >= 0; i--) {

    println(arr[i]);

}

400

What is the output of the following program?

function start(){
    var arr = [12, 17, 65, 7, 30, 88];
    var elem = arr.pop();
    println(arr);
    println(elem);
}

[12, 17, 65, 7, 30]
88

400

Sarah works at a store and is preparing receipts for customers. For each customer purchase, she needs to generate two copies of the receipt: one for the customer and one for the store’s records. Write a function that takes a list of receipts and returns a new list where each receipt appears twice in a row.

function doubleReceipts(receipts) {

    let newReceipts = [];

    for (let i = 0; i < receipts.length; i++) {

        newReceipts.push(receipts[i], receipts[i]);

    }

    return newReceipts;

}

400

Consider the code segment. What will be the output?

function start(){
    var arr = [];
    arr.push(5);
    arr[1] = 12;
    println(arr[0]);

    arr.push("hello");
    arr.push(true);
    arr.push(1000);
    arr.push(3.2);

    println(arr[3]);
    var last = arr.pop();
    println(last);
    println(arr[4]);
}

5
true
3.2
1000

500

Ms. Johnson, a high school teacher, just finished grading her students’ latest math test. She wants to recognize students who scored well by creating an honor list of those who earned 85 or higher. Help her write a function goodGrades that takes a list of student scores and returns only the high-achieving grades.

function goodGrades(grades) {

    let goodGradesList = [];

    for (let i = 0; i < grades.length; i++) {

        if (grades[i] >= 85) {

            goodGradesList.push(grades[i]);

        }

    }

    return goodGradesList;

}

500

Liam is training for a marathon and tracks his running distances each day. To measure his overall performance, he wants to calculate the average distance he runs per day. Write a function averageDistance that takes an array of distances (in miles) and returns the average distance run.

function averageDistance(distances) {

    let sum = 0;

    for (let i = 0; i < distances.length; i++) {

        sum += distances[i];

    }

    return sum / distances.length;

}

500

Suppose we have a list groceryList shown below:

var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"];

Once we buy a certain item, we should remove it from the list to show that we bought it. We want to write a function removeGrocery that will take an item as a parameter and remove it from our grocery list.

function removeGrocery(groceryList, item){
    var index = groceryList.indexOf(item);
    if(index != -1){
        groceryList.remove(index);
    }
}

500

Suppose we have a list groceryList shown below:

var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"];

Write a function addGrocery that will take a grocery list and an item as a parameter and add the item on to the end of the grocery list.

function addGrocery(groceryList, item){
    groceryList.push(item);
}

500

We want to be able to read our grocery list in a readable format while we’re shopping. Write a function printGroceries such that the following code:

var groceryList = ["milk", "bread", "eggs", "sugar", "carrots", "apples"];
printGroceries(groceryList);

Should print out

1. milk
2. bread
3. eggs
4. sugar
5. carrots
6. apples

function printGroceries(groceryList){
    for(var i = 0; i < groceryList.length; i++){
        println((i + 1) + ". " + groceryList[i]);
}

M
e
n
u