**Unformatted text preview:**

CS 105Study GuideSpring 2014JavaScriptExamples of functions- Function that sums cookiesfunction sumCookieTypes(cookie1, cookie2, cookie3, cookie4){ var result = cookie1 + cookie2 + cookie3 + cookie4; return result; }---or---{return cookie1+cookie2+cookie3+cookie4;}- Counts unique cookies (types of cookies)function uniqueCookieTypes(cookie1, cookie2, cookie3, cookie4){ var count = 0 if (cookie1 == 0) count = count; else count = count + 1; if (cookie2 == 0) count == count; else count = count + 1; if (cookie3 == 0) count == count; else count = count + 1; if (cookie4 == 0) count == count; else count = count + 1; return count;}---or---{var total=0;if (cookie1>0){total=total+1;}if (cookie2>0){total=total+1;}if (cookie3>0){total=total+1;}if (cookie4>0){total=total+1;}return total;}- Cost of cookies (cookie1=$4, cookie2, cookie 3 and cookie4= $3.50) function totalPrice(cookie1, cookie2, cookie3, cookie4){return 4*cookie1 + 3.5*cookie2 + 3.5*cookie3 + 3.5*cookie4;}- If a window prompts you to enter a number/value in a box, this is an example of JS behind itfunction foo(value){if(value==1){return “You entered the value 1”;}if(value!=1){return “You did not enter the value 1”;}}- Nested if statements{if(value>=10){if (value<=30){return “Your value is between 10 and 30”;}}return “Your value is not between 10 and 30”;}*both if statements need to be true for nested statements. If only one statement is true, butnot the nested one, that function won’t run.- Counting down from 10 to 0{var value=10;while (value!=10){value=value-1;}}*while function will keep running until whatever follows while in the parenthesis is no longer true- Using && with if statementsif (temp<30 && temp>10){wear hat();}---or---if ((temp<30) && (temp>10)){wear hat();}- Counting how many days the temperature was below the cutofffunction zeroDegreeDays(temps, cutoff){var counter = 0for (var i=0; i<temps.length; i++){if (temps[i] < cutoff){counter++;}}return counter;}JavaScript notation- x=x+4 x+=4- x=x+1 x++- x=x-1 x--- x=x*1 x*=1- x=x/1 x/=1- x** and x// do NOT exist- && and- || or- use = to define a variable, but otherwise use ==- != not equal to- >= greater or equal to- <= less than or equal to- “ ” empty string or a space- .charAt() tells you what character is at a certain spoto var s = “Dog” (“d” is at spot 0, “o” is at spot 1, and “g” is at spot 2)var c = s.CharAt(1)so c = o- .charCodeAt() tells you the ASCII value at a certain spot- .length is number of elements within an array or stringFor function- For function is similar to a while functiono for (i=0; i<t.length; i++) First part is defining the variable of (e.g. i=0) Second part is creating a condition that needs to be true for the function to run (e.g. i<t.length i<7, i>10) Third part is the stunt that happens when the condition is true (i++,i--)Arrays- Lists/arrays start with brackets and can include numbers, strings, and arrayso var nums = [2,7,4,3,8]o var str = [“Hello”, “what”, “okay”]o var arr = [[60,4], [7,9],[80,90]]Formatting- Curly braces only follow after while, if, function, else, for- Parenthesis describe the logic associated with while, if, function, else, for- Square brackets ONLY with arraysSorting- Binary search o Faster than linear searcho Needs to be sortedo Goes to halfway point and then removes half where searchable value is notfound (repeats until you find the value)- Selection sort puts an array in alphabetical or ascending ordero Takes the element that should be first and swaps places with what’s in the first spot 6, 1, 4, 5 6 and 1 swap places 1, 6, 4, 5 Continue swapping until sorted- Linear search works for any (unsorted OR sorted) array but is slower because it goes through every elementOther info- When you have a something in quotes, it loses it numerical value- Computer only knows about the digits 0,1o Use binary to avoid using 2,3, 168, etc.- ACII uses numbers to represent letters and characters- Pixels are made up of RGBs red, green, and blueo #FF0000 is all red, #00FF00 is all green, #0000FF is all blueo #000000 is black, #FFFFFF is whiteBinary to Decimal- First start with 0, then multiple 0 by 2 (it’s always 2), then lastly add whatever thefirst binary number is to that. The product you get from this first step is what you’ll use to start for the next step. - 00011 3o First, we always start with 0.o 0 * 2 + 0 = 0 (we carry this product of 0 over to the next step)o 0 * 2 + 0 = 0 (we carry this 0 over to the next step)o 0 * 2 + 0 = 0 (we carry this 0 over to the next step)o 0 * 2 + 1 = 1 (we carry this 1 over to the next step)o 1 * 2 + 1 = 3o 3 is the correct answer!o If you look at the second column of numbers, they are all 2o If you look at the third column of numbers it is 00011 which is our binary number- 1110 14o 0 * 2 + 1 = 1o 1 * 2 + 1 = 3o 3 * 2 + 1 = 7o 7 * 2 + 0 = 14Excel- =count(range)o Counts the number cells selected that contain numbers- =counta(range)o Counts the number of cells that have anything in it (like letters, characters)- =sum(range)o Sums the values of cells selected- =average(range)o Averages the values of cells selected- =countif, =countifs, =sumif, =sumifs, =averageifo All have a criteriao Criteria needs to be within quotes “>30” or “=10”o To reference another cell within your criteria, need an & “=”&G4 or “<”&C3 or “>”&A2 “<>”&C2 means NOT equal to C2o Can use * to help with criteria as well “*snow” includes comments that end with “snow” “snow*” includes comments that start with “snow” “*snow*” includes any comments with “snow” in it “*”&C4&“*” if referencing a cell- =vlookup(lookup_value, table_array, col_index_num, [range_lookup]))o Returns a value based on lookup_valueo Table_array selects the data that includes the lookup_value and the value you want returned The value you want returned has to be to the LEFT of the lookup_valueo Col_index_num is the column that has the value you want returnedo If fourth parameter is FALSE returns an exact matcho If fourth parameter is TRUE, then the leftmost column of table_array needs to be sorted as

View Full Document