Arrays

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

index

a position in an array

How can we get the length of an array arr?

arr.length

To add all numbers in an array

create a variable sum : var sum = 0; create a for loop, add each element to the sum: for(var i = 0; i<arr.length; i++){ var cur= arr[i]; sum+=cur; } println(sum); }

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); }

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

indexOf

What index refers to the "end" of an array?

the highest index

how to make an empty array

var arr= [ ]; square brackets empty

How to create an array?

var arr= [1,2,3,4,5] ; var shoppingList= ["bread", "eggs", "milk"];

How to get array length (# items in an array)?

var length= arr.length; arr is name of the array if you had an array called shoppingList: var length= shoppingList.length;

var numbers = [0, 1, 2, 3, 4, 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();

model

A set of simplifying assumptions about some aspect of the real world.

What is a benefit of a simulation?

mimics real world events without cost or danger

var numbers = [0, 1, 2, 3, 4]; How can we add a 5 to the end of numbers?

numbers.push(5);

to print an item in an array

println(arr[0]) prints the item at index 0 of arr

How to change an index of an array?

shoppingList[2] = "cake" ; changes the item at index 2 to cake

var shoppingList = ["milk", "eggs", "sugar", "bread", "cake"]; Which line of code will correctly change "cake" to "apples"?

shoppingList[4] = "apples";

var groceries = ["milk", "eggs", "bread", "cheese", "yogurt", "sugar"]; What will be the result of groceries.indexOf("cake"); ?

-1, when an item doesn't exist in the array, the index is returned as -1

What does Javascript index start at vs AP lang start?

AP- index starts at 1 JAVA- index starts 0

What is an array (or list)?

An ordered collection of items

How to print every element in an array?

First have the array created: var arr= ["bread", "milk", "cookies", "cheese"]; then create a for loop: for(var i = 0; i< arr.length; i++){ var cur= arr[i]; println(cur); }

Which of the following statements are true about simulation models? I - Models often omit unnecessary features of the phenomena being modeled.II - The time it takes for a simulation to run increases as the underlying model becomes more complex.

I and II

var groceries = ["milk", "eggs", "bread", "cheese", "yogurt", "sugar"]; What will be the result of groceries.indexOf("yogurt"); ?

4

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

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, 2, 4, 5, 6, 7]

simulation

an imitation of a possible situation

What kinds of items can we store in arrays?

any kind of object (anything you can store in a variable)

to remove the last element in the array

arr.pop( ); you dont need to put anything in parenthesis if you save the popped item into a variable, you can access it later ex) var last = arr.pop( );

How to add an element to the array?

arr.push( ); whatever you want to add to the end of list put in ( ). for example if you want to add cake to the 0 index arr.push("cake"); arr.push("bread"); now index 0= cake and index 1 = bread

How to remove 1 element?

arr.remove(i); i=index arr.remove(2); removes element at index 2

How to remove at a certain index one or more?

arr.splice(2,1); remove starting from index 2, 1 element

Model complexity vs running time

the simpler the model, the faster it runs the more complex, the more computing power needed, takes longer


Kaugnay na mga set ng pag-aaral

Introduction to Joint Multi-TDL Network (MTN) Operations JT101

View Set

Chapter 8 Test: Emergency Care, First Aid, & Disasters Practice Questions

View Set

Spanish Subject Pronoun Replacement

View Set

Education 300 Final JMU, Education 300 Final, Education 300 Midterm JMU Ch. 1, 11, 10, 6, 5

View Set

AP Environmental Science Key Terms

View Set

Notes 4, Chpt 3 Pt 3 - 422: Integrity Constraints

View Set

Chapter 10 Fetal Development and Genetics

View Set