Unit 5 Lists, Loops, and Traversals

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

a-0 REPEAT 3 TIMES a-a+1 DISPLAY a a-a+1

" 1 3 5"

count-0 REPEAT UNTIL count=3 count- count + 1 DISPLAY "and" DISPLAY count

"and 1 and 2 and 3"

What will myStuff[1] do? var myStuff = [20,"hat","pow",5]; myStuff[1] = "cat"; myStuff[2] = myStuff[1]; myStuff[0] = myStuff[3]+10; myStuff[3] = myStuff[0]+myStuff[0];

"cat"

What will myStuff[2] do? var myStuff = [20,"hat","pow",5]; myStuff[1] = "cat"; myStuff[2] = myStuff[1]; myStuff[0] = myStuff[3]+10; myStuff[3] = myStuff[0]+myStuff[0];

"cat"

gems-"rubby", "sapphire","garnet" gems- "opal", "emerald" DISPLAY gems2

"emerald"

var myNumbers = [32, 64, 33, 0, 15, 26, 3] myNumbers[3]

0

var newList = [10, 20, 30]; What will this do: insertItem(newList, 1, 15);

10 15 20 30

What will myStuff[0] do? var myStuff = [20,"hat","pow",5]; myStuff[1] = "cat"; myStuff[2] = myStuff[1]; myStuff[0] = myStuff[3]+10; myStuff[3] = myStuff[0]+myStuff[0];

15

var myNumbers = [32, 64, 33, 0, 15, 26, 3] myNumbers[4]

15

What will appendItem(myStuff, 60); appendItem(myStuff, 80); do? var myStuff = [20,"hat","pow",5]; myStuff[1] = "cat"; myStuff[2] = myStuff[1]; myStuff[0] = myStuff[3]+10; myStuff[3] = myStuff[0]+myStuff[0];

15 "cat" 30 60 80

What will removeItem(myStuff, 2); do? var myStuff = [20,"hat","pow",5]; myStuff[1] = "cat"; myStuff[2] = myStuff[1]; myStuff[0] = myStuff[3]+10; myStuff[3] = myStuff[0]+myStuff[0];

15 "cat" 30 undefined

What will myStuff[3] do? var myStuff = [20,"hat","pow",5]; myStuff[1] = "cat"; myStuff[2] = myStuff[1]; myStuff[0] = myStuff[3]+10; myStuff[3] = myStuff[0]+myStuff[0];

30

var myNumbers = [32, 64, 33, 0, 15, 26, 3] myNumbers[0]

32

luckyNumbers- 15, 33, 25 INSERT luckyNumbers, 2, 13 APPEND luckyNumbers, 3 REMOVE luckyNumbers, 1 DISPLAY LENGTH luckyNumbers

4

var myNumbers = [32, 64, 33, 0, 15, 26, 3] myNumbers[1]

64

When breaking a problem down, you often encounter elements that you want to use repeatedly in your code. Sometimes it's appropriate to write a new function; at other times it's appropriate to write a loop. There is no hard-and-fast rule as to which is better, but what do you think? What kinds of circumstances would lead you to writing a function versus using a loop?

A function is better for a set of specific tasks that need to be completed in the movement while a loop should be used to repeat things. Thus I think that programming is more about specific tasks than repeated labor so functions are better.

appendItem(list, item)

Adds an element to the end of the list A new index is added to the list to create a place for the element The new item is placed in this new index

What is a loop an example of?

An iteration

myList[1] = "hello"

Assigns the value on the right to the index Just like variable assignment, the old value is thrown away and replaced

var myNumbers = [10,20,25]; myNumbers[2] = 30; Discuss what the list will contain after line 02 runs.

Changes to 30

What is a For Loop?

Condenses the parts of a while loop into a shorter statement. Similar to the while loop, once the Boolean expression becomes false, the loop ends.

What do each element has?

Each element has its own index.

What happens each time we pass through the loop?

Each pass through the loop, the counting variable changes - usually by one. We can access each element by evaluating list[i] in the for loop.

How does a while loop work?

Executes a statement repetitively based on a boolean expression

What can we do when we know the element at each spot in a list?

Filter (create a subset of elements from the original list) Reduce (reduce the list down to a single element, for example: the smallest number in the list)

What is the length of the list?

How many elements it contains.

With a partner, discuss the challenges that you'd encounter if you need to store 10, or 100, or 1,000 pieces of information using variables.

If you would need to store that much data in variables then you would have to make 10, 100, 1000 variables just to access the info. Lists can access all in short amount of code.

What is a variable?

In computer programming, a variable or scalar is a storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value.

What is an abstraction?

In software engineering and computer science, abstraction is: • the process of removing physical, spatial, or temporal details or attributes in the study of objects or systems to focus attention on details of greater importance

Where do indexes start?

Indexes start at zero and count up.

insertItem(list, index, item)

Inserts an element into a list at the index given A new index is added to the list so there's space for the new element The new element is placed at the index given, all other items move right

var nums = [10,50]; insertItem(nums,1,20); insertItem(nums,1,100); Discuss what the list will contain after line 02 runs.

It places 100 in the middle of 10 and 50.

What are lists and example of?

Lists are an example of data abstraction. They allow us to name and program with large collections of information while ignoring the low level details of how the data is stored, organized, etc. These programs are easier to develop and maintain.

What are lists made out of?

Lists are made out of elements.

What can lists contain?

Lists can contain different types of data, including numbers and strings.

Think back on the Font Tester App. Can you think of an example of another app or feature of an app which would use a loop to control different elements on a screen?

Music programs often use programmed loops to execute the same thing over and over again. Gifs also use loops as they loop the images back and forth. Next, there are gambling programs that loop the slot machine or certain numbers.

var myNumbers = [10,20,25]; 01 removeItem(myNumbers,1); 02 removeItem(myNumbers,0); Do This: Discuss what the list will contain after line 02 runs.

Removes 10

removeItem(list, index)

Removes the element in the given list at the given index All items to the right are shifted over The last index is removed from the list

How does a list manage complexity in a program?

Store multiple pieces of info items in list mult times

What does i++

The counter variable i increases after each loop runs.

Length of list

The length of a list is how many elements it contains. Lists can grow or shrink as elements are added or removed.

What are some similarities and differences between lists and variables?

They both store information that can be called back and programmed to be changed but lists store more information while variables can't and therefore can have more complex changes than variables.

In the i<list.length...

This is the Boolean expression that is checked to see if the loop should continue to run. When working with a list, we want to access every element in the list, so we are going to keep the loop going until we have hit list.length.

In the for loop var i=0...

This is where we assign the starting value to the counter variable i. Usually we assign it to 0. What else starts with 0? The index of a list!

What do you do to access each element in the list?

To access each element in the list, we are going to store the element in a variable inside the for loop. Let's try this out with your Traversal Machine.

How to debug lists?

Use console.log to track lists in the Debug Console. Click the arrow to see elements listed by index.

What is a While Loop?

Uses a boolean condition to repeatedly run a block of code. If it is true it runs the block of code contained within it. This process of checking the condition and running the block of code is repeated as long as the Boolean condition remains true. Once the Boolean expression becomes false it will stop.

Traversal

We are traveling or traversing through a list one element at a time. the process of accessing each item in a list one at a time

Iteration

a repetitive portion of an algorithm which repeats a specified number of times or until a given condition is met.

00 var myNumbers = [10]; 01 appendItem(myNumbers,50); 02 appendItem(myNumbers,100); Discuss what the list will contain after line 02 runs.

adds 100 to the list

index

common method for referencing the elements in a list or string using numbers

myString.length

give length of string

With a partner, brainstorm lists of information that you encounter on a daily basis. Why are these lists useful?

grocery list, to do list, list of grades good for organization

Element

individual value in a list that is assigned a unique index

What will this do: var myStuff = [20,30,15,105]; for(var x=0;x<myStuff.length;x++){ console.log(myStuff[x]);

it will log myStuff.

luncfood( "nachos, "pizza", "soup") What do lunchFood[0] and lunchFood[2] access?

nachos and soup

infinite loop

occurs when the ending condition will never evaluate to true.

What is an example of a correct for loop?

onEvent("button1", "click", function( ) { for (var i = 0; i < 100; i++) { penRGB(randomNumber(0, 255), randomNumber(0, 255), randomNumber(0, 255)); penDown(); dot(randomNumber(1, 10)); penUp(); moveTo(randomNumber(10, 310), randomNumber(10, 440)); } });

What is an example of a correct while loop?

onEvent("button2", "click", function( ) { var x = 0; while (x<100) { penRGB(randomNumber(0, 255), randomNumber(0, 255), randomNumber(0, 255)); penDown(); dot(randomNumber(1, 10)); penUp(); moveTo(randomNumber(10, 310), randomNumber(10, 440)); x = x+1; } });

List

ordered collection of elements

myString.substring(start,end)

returns a substring starting at start, and going up to but not including end

What do we do when using a for loop?

to traverse a list, visiting each element one at a time.


Kaugnay na mga set ng pag-aaral

Pelvis Structure & Compartmentalization: Pelvis (Dang)

View Set

National Spanish Exam- Gender of nouns ending in dad, cion, pa, ma, site

View Set

Chapter 1 Role of the Project Manager/PMBOK Guide Overview

View Set

chapter 20 test review questions

View Set