Arrays II Robin Burke IT 130
Outline Homework #6 Portfolio Array review Array examples Array algorithms Multi-dimensional arrays
Homework #6 Three component grades Knowledge Reasoning Communication Overall very good papers! Many nice site designs
Issues Target marketing not really possible for campaign web site you have to identify the audience that will use such a site not choose the audience and build site to suit Narrow content site has only policy positions and events easy to find these things real site would have much more Design justification report should justify design choices example: information hierarchy each page has foreground and background what belongs in each category what visual techniques distinguish
Portfolio Must include new copies of each homework exceptions if full credit, just copy homework #6 – report only MS Word.doc or PDF file no "original" needed Do not include labs homework #6 designs / notes Due date Midnight, 11/22 (Monday before Thanksgiving)
Portfolio Do Get started early you now have enough experience that early assignments should be easy (or at least easier) Aim for full credit on all assignments send me if you don't understand the grading feedback Read the original assignment Don't Wait until the last minute Ignore what the grader had to say
Portfolio site it130 hwk1 hwk2...etc... portfolio index.html points to old and new assignments hwk1 hwk2 hwk3...etc.. Example
Array Array is a list of values accessed by index name[index] foo[5] Must create before using foo = new Array (); Zero-based indexing length attribute arr.length
Using for loop to access array For loop pieces initial counter value: 0 exit test: value < array.length change to counter: + 1 Pattern for (i = 0; i < arr.length; i++) { do something to arr[i] element }
Images array twodice page from last week document.images[imgNo].src = "dice01.jpg"; How to read this from the document object the page get the images array a list of all the images on the page get one item from this array using imgNo as index some particular image set the src attribute of this image to the given value
Example n dice
Array algorithms Common tasks with arrays find an element with some property combine the elements of the array fill an array with values sort the elements
Combination example: average var total = 0; for (i = 0; i < numbers.length; i++) { total = total + numbers[i]; } ave = total / numbers.length;
Combination pattern Assume array contains some values var variable = empty value ; for (i = 0; i < array.length; i++) { variable = accumulate ( variable, array[i] ); }
Example build-sentence.html Assemble a sentence using the words in an array words = new Array ("welcome", "to", "arrays", "in", "JavaScript"); function result = "welcome to arrays in JavaScript"
Selection example Find the first element in the array less than zero for (i = 0; i < array.length; i++) { if (array[i] < 0) return i; } return -1;
Selection pattern for (i = 0; i < array.length; i++) { if ( test (array[i])) return i; or return array[i]; } return not found value ;
Selection Example Find the smallest element of an array A bit different from pattern We have to look at every element save the best so far
Fill example Fill an array with zeros for (i = 0; i < count; i++) { array[i] = 0; }
Fill pattern for (i = 0; i < size ; i++) { array[i] = initial value ; }
Example Alternate dice idea Fill array with image urls Pick from array instead of if statement Usually a good idea
Parallel arrays Sometimes we have data that is paired state / # of electoral votes homework / date due We can handle this with two arrays same size corresponding indices mean corresponding data
Example duedate.html
Problem Easy for arrays to get out of sync Two things to keep track of The pairing is in the programmer's head not represented in the structure of the data
Multi-dimensional array We can change our data structure instead of two arrays of elements, which are paired we can have one array of pairs Example
Homework #8 Slide show Add rewind, fast-forward, and back buttons Use enable / disable Write a function that controls the buttons so enable / disable logic is not repeated
Monday Data representation