eg-259:lecture6
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
eg-259:lecture6 [2013/02/07 20:37] – [Functions are Objects] eechris | eg-259:lecture6 [2013/02/07 21:09] (current) – [Exercises] eechris | ||
---|---|---|---|
Line 622: | Line 622: | ||
* It is given a name by assigning it to a variable -- in this case '' | * It is given a name by assigning it to a variable -- in this case '' | ||
* This makes explicit the fact that a function is just another primitive in JavaScript. | * This makes explicit the fact that a function is just another primitive in JavaScript. | ||
- | * You'll see this form used a lot in [[http://codeyear.com|CodeYear]] and in event handlers to come later. | + | * You'll see this form used a lot in the [[http://www.codecademy.com/ |
===== Another Example ===== | ===== Another Example ===== | ||
- | * [[/eg-259/examples/lecture6/medians.html|medians.html]] | + | * [[http://jsfiddle.net/cpjobling/ |
---- | ---- | ||
Line 639: | Line 638: | ||
<code javascript> | <code javascript> | ||
- | < | + | // medians.js |
- | < | + | // A function and a function tester |
- | A function and a function tester | + | // Illustrates array operations |
- | Illustrates array operations | + | |
- | --> | + | |
- | <html lang=" | + | |
- | < | + | |
- | <meta charset=" | + | |
- | < | + | |
- | < | + | |
- | /* Function median | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | */ | + | |
- | var median = function (list) { | ||
- | list.sort(function(a, | ||
- | return a - b; | ||
- | }); | ||
- | var list_len = list.length; | ||
- | // Use the modulus operator to determine whether | + | var element = $('# |
- | // the array's length is odd or even | + | |
- | // Use Math.floor to truncate numbers | + | |
- | // Use Math.round to round numbers | + | |
- | if((list_len % 2) == 1) { | + | /* |
- | return list[Math.floor(list_len | + | Function median |
- | } | + | Parameter: An array of numbers |
- | else { | + | Return value: The median of the array |
- | return Math.round((list[list_len / 2 - 1] + list[list_len / 2]) / 2); | + | */ |
- | } | + | |
- | }; // end of function median note semicolon | + | |
- | </ | + | |
- | </ | + | |
- | < | + | |
- | < | + | |
- | var my_list_1 = [8, 3, 9, 1, 4, 7]; | + | |
- | var my_list_2 = [10, -2, 0, 5, 3, 1, 7]; | + | |
- | var med = median(my_list_1); | + | |
- | document.write(" | + | |
- | med = median(my_list_2); | + | |
- | document.write(" | + | |
- | </ | + | |
- | </ | + | |
- | </ | + | |
- | </code> | + | |
+ | var median = function (list) { | ||
+ | list.sort(function (a, b) { | ||
+ | return a - b; | ||
+ | }); | ||
+ | var list_len = list.length; | ||
+ | |||
+ | // Use the modulus operator to determine whether | ||
+ | // the array' | ||
+ | // Use Math.floor to truncate numbers | ||
+ | // Use Math.round to round numbers | ||
+ | |||
+ | if ((list_len % 2) == 1) { | ||
+ | return list[Math.floor(list_len / 2)]; | ||
+ | } else { | ||
+ | return Math.round((list[list_len / 2 - 1] + list[list_len / 2]) / 2); | ||
+ | } | ||
+ | }; // end of function median -- note semicolon | ||
+ | |||
+ | var my_list_1 = [8, 3, 9, 1, 4, 7], | ||
+ | my_list_2 = [10, -2, 0, 5, 3, 1, 7], | ||
+ | med; | ||
+ | |||
+ | med = median(my_list_1); | ||
+ | element.append("< | ||
+ | |||
+ | med = median(my_list_2); | ||
+ | element.append("< | ||
+ | </ | ||
Line 703: | Line 693: | ||
myPlane = new Plane(" | myPlane = new Plane(" | ||
</ | </ | ||
+ | |||
+ | [[http:// | ||
---- | ---- | ||
Note by convention, constructors use upper case names to distinguish them from ordinary functions. Think //proper noun//. | Note by convention, constructors use upper case names to distinguish them from ordinary functions. Think //proper noun//. | ||
- | |||
===== Method Properties ===== | ===== Method Properties ===== | ||
Line 782: | Line 773: | ||
===== Exercises ===== | ===== Exercises ===== | ||
- | Write, test and debug (if necessary) | + | Use jsFiddle or some other method to write, test and debug (if necessary) |
- //Output//: A table of numbers from 5 to 15 and their squares and cubes, using alert. | - //Output//: A table of numbers from 5 to 15 and their squares and cubes, using alert. | ||
- | - //Output//: The first 20 Fibonacci numbers, which are defined as in the following sequence 1, 1, 2, 3, … where each number in the sequence is the sum of the two previous numbers. Use '' | + | - //Output//: The first 20 Fibonacci numbers, which are defined as in the following sequence 1, 1, 2, 3, … where each number in the sequence is the sum of the two previous numbers. Use '' |
- Modify the script of Exercise 2 to input a number, //n//, using '' | - Modify the script of Exercise 2 to input a number, //n//, using '' | ||
- // | - // |
eg-259/lecture6.1360269438.txt.gz · Last modified: 2013/02/07 20:37 by eechris