Unit 5 Assessment Review

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Key Takeaways of Lists

* The length of a list is how many elements it contains. Lists can grow or shrink as elements are added or removed. * 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.

Filtering One List

// Create and assign list var animals = ["alligator","bird","cow","dog","emu","flamingo","gorilla","hippo"]; console.log("Which animals have more than 5 letters in their names?"); // Filter animals to those with more than 5 letters var animalsFiltered = []; var animal; for(var i = 0; i < animals.length; i++){ animal = animals[i]; if(animal.length > 5){ appendItem(animalsFiltered,animal); } } console.log(animalsFiltered);

Loop

A loop is an example of iteration: a repetitive portion of an algorithm which repeats a specified number of times or until a given condition is met.

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?

A photo editor app may use loops to control properties such as image size, image colors, image brightness, etc.

Why is traversal with a for loop a good method for accessing and updating items in a lengthy list?

A traversal with a for loop is a good method for accessing and updating items in a lengthy list because every element in the list is visited, one at a time. The list may be filtered so that there is a subset of elements from the original list or even may be reduced to a single element on the list.

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 last index is removed from the list 00 var myNumbers = [10]; 01 appendItem(myNumbers,50); 02 appendItem(myNumbers,100);

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. for(var i=0; i<3; i++){ ... do something }

List Reduce Pattern

How it works - Create a list of numbers - Create a blank variable to store the smallest number In a function create a local variable to store a temporary value - Use a for loop to access each element in the list - If that element is less than the value stored in temp, set the value of temp to the current element - After the for loop set the value of smallest to the value stored in temp // Setting up variables var maxPrice = prices[0]; // Traverse the list to find the maximum value for (var i = 0; i < prices.length; i++) { // Add code here price = prices[i]; if(price > maxPrice){ maxPrice = price; } } console.log("The maximum price is $" + maxPrice);

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?

I think functions should be used in circumstances where the user is performing an input like a button click, or on events. Within functions, loops may be used to set the property of an element depending on the iteration of the loop. For instance, a user may click a button in the program (function), and for every iteration, the screen color is randomized (loop).

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 00 var nums = [10,50]; 01 insertItem(nums,1,20); 02 insertItem(nums,1,100); 01 [ 10, 20, 50] 02 [ 10, 100, 20, 50 ]

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 00 var myNumbers = [10,20,25]; 01 removeItem(myNumbers,1); 02 removeItem(myNumbers,0);

Explain how you would filter the dog dataset using traversal to have a filtered list of dogs who live long lives.

Similar to how the dog dataset was filtered into small, medium, and large dogs according to dog height on Level 4, I would use a traversal to filter the dataset into dogs who live short, average, and long lives according to their maximum life span. The for loop would filter the list by labeling dogs who live a maximum life span of 10 years or less as dogs who live a short life. Dogs who live a maximum life span greater than 10 and less than 16 would be filtered into the list of dogs with an average life span. Lastly, dogs with a maximum life span of 16 years or greater would be filtered into a list of dogs who live long lives.

List Length

The length of the list is how many elements it contains. This list has 3 elements and indexes from 0 to 2.

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. var count = 0; while(count < 3){ ... do something count++; }

Traversals Key Takeaways

We use a for loop to traverse a list, visiting each element one at a time. 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. Once we know the element at each spot in a list, we can do different things with it: 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)

00 var myStuff = [20, "hat", "pow", 5]; 01 myStuff[1] = "cat"; 02 myStuff[2] = myStuff[1]; 03 myStuff[0] = myStuff[3] + 10; 04 myStuff[3] = myStuff[0] + myStuff[0];

[ 15 , "cat" , "cat" , 30 ]

create a list using . . .

[ ] square brackets

Index

a common method for referencing elements in a list or string using numbers (indexing starts at 0)

Function

a named group of programming instructions. Also referred to as a "procedure". can be called using a function call

Iteration

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

00 var aList = [20, "hat", "pow"]; 01 appendItem(aList, 5); 02 appendItem(aList, 10); 03 removeItem(aList, 1); 04 insertItem(aList, 2, "bang");

aList [ 20, "pow", "bang", 5, 10]

Variables

an abstraction inside the program that can hold a value. Each variable has associated data storage that represents one value at a time.

Element

an individual value in a list that is assigned a unique index

List

an ordered collection of elements, a collection of organized items

00 var bList = ["to", 5, "po"]; 01 appendItem(bList, bList[2] + "ta" + bList[0]); 02 insertItem(bList, 2-1, "go"); 03 removeItem(bList, 2);

bList [ "to" , "go", "po", "potato" ]

Filter

create a subset of elements from the original list

A for loop combines the 3 parts of a while loop into one statement

for (var i=0; i<4; i++){ moveForward( ); }; A loop is an example of iteration: a repetitive portion of an algorithm which repeats a specified number of times or until a given condition is met.

Update an index's value

names [0] = "Sarah" replaces James @ index 0

You can get the length of the list by using

numList.length --> list length may be 6, but indexing ends at 5 * We typically find the index of the last element by using the expression numList.length - 1

infinite loop

occurs when the ending condition will never evaluate to true.

var Random List Access Pattern

on event user interacts with program, something is randomized between 0 and list.length-1

List Scrolling Pattern

onEvent("leftTopButton","click",function(){ if(topsIndex > 0){ topsIndex = topsIndex - 1; } updateScreen(); }); onEvent("rightTopButton","click",function(){ if (topsIndex < topsList.length-1) { topsIndex = topsIndex + 1; } else { topsIndex = 0; } updateScreen(); });

Reduce

reduce the list down to a single element, for example: the smallest number in the list

Traversal

the process of accessing each item in a list one at a time for (var i = 0; i < temps.length - 1 ; i++){ var element = temps [ i ]; };

Strings have indexes too!

var myString = " T h i s o r T h a t " 0 1 2 3 4 5 6 7 8 9 10 11 - myString.length will give the length of the string, which is 12 - myString.substring(start,end) returns a substring starting at start, and going up to but not including end - myString.substring(0,4) is "This" - myString.substring(5,7) is "or"

While Loop: 3 parts

var steps = 0; a counting variable set to a initial value while ( steps < 4 ) { boolean ex. that checks condition of variable moveForward ( ); steps ++ ; A statement which increases or decreases the variable that is being checked. Note: if this piece is missing, you may create an infinite loop that never stops running, or crashes your browser! } ;

Filtering Multiple Lists

var students = ["Angelo","Emilia","Abel","Darien","Elly"]; var grades = [11, 10, 12, 10, 9]; // Traversing both lists to print the grade of each student var student; var grade; for(var i = 0; i < students.length; i++){ student = students[i]; grade = grades[i]; console.log(student + " is in grade " + grade); }

Lists v. Variables

variables are useful for storing a single piece of information, but as we collect more information keeping the variables organized can be complicated and cumbersome

To access an individual item in a list . . .

write out the name of the list and the number of the index you want to access Ex: names [2]


Set pelajaran terkait

Real Estate Practices - Chapter 3: Agency and other Mandatory Disclosures

View Set

OLS-34200 Interview Strat Orgn - Midterm

View Set

Crozer Podiatry Manual 2nd Edition

View Set

The Fight Against Racial Injustice is Transatlantic

View Set

Developmental Concepts - OB Module 3

View Set

The CITI Good Clinical Practice Course for Clinical Trials Involving Drugs and Biologics Quiz

View Set

Entrepreneurship and Small Business Certification Exam

View Set