**Unformatted text preview:**

CS 105 Study Guide Spring 2014 JavaScript Examples of functions Function that sums cookies function 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 total total 1 var total 0 if cookie1 0 if cookie2 0 if cookie3 0 if cookie4 0 total total 1 total total 1 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 it function foo value if value 1 if value 1 return You entered the 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 but not the nested one that function won t run Counting down from 10 to 0 var value 10 while value 10 value value 1 Using with if statements if temp 30 temp 10 wear hat or if temp 30 temp 10 wear hat while function will keep running until whatever follows while in the parenthesis is no longer true Counting how many days the temperature was below the cutoff function zeroDegreeDays temps cutoff var counter 0 for 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 spot o 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 string For function For function is similar to a while function o 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 Formatting Lists arrays start with brackets and can include numbers strings and arrays o var nums 2 7 4 3 8 o var str Hello what okay o var arr 60 4 7 9 80 90 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 arrays Sorting Binary search o Faster than linear search o Needs to be sorted o Goes to halfway point and then removes half where searchable value is not found repeats until you find the value Selection sort puts an array in alphabetical or ascending order o 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 element Other info When you have a something in quotes it loses it numerical value Computer only knows about the digits 0 1 o 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 blue o FF0000 is all red 00FF00 is all green 0000FF is all blue o 000000 is black FFFFFF is white Binary to Decimal First start with 0 then multiple 0 by 2 it s always 2 then lastly add whatever the first 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 3 o 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 3 o 3 is the correct answer o If you look at the second column of numbers they are all 2 o If you look at the third column of numbers it is 00011 which is our binary number 1110 14 o 0 2 1 1 o 1 2 1 3 o 3 2 1 7 o 7 2 0 14 Excel count range counta range sum range average range o Counts the number cells selected that contain numbers o Counts the number of cells that have anything in it like letters characters o Sums the values of cells selected o Averages the values of cells selected countif countifs sumif sumifs averageif o All have a criteria o 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 C2 o 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 value o Table array selects the data that includes the lookup value and the value you want returned lookup value The value you want returned has to be to the LEFT of the o Col index num is the column that has the value you want returned o If fourth parameter is FALSE returns an exact match o If fourth parameter is TRUE then the leftmost column of table array needs to be sorted as ascending Faster than when the fourth parameter is FALSE index array row num o Returns a value based on the index row number match lookup value lookup array match type o Returns the index of the value you re looking up o Match type of 0 returns an exact match doesn t need to be sorted data o Match type of 1 returns the largest value that is …

View Full Document