User Tools

Site Tools


eg-259:lecture6

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
eg-259:lecture6 [2013/02/07 20:37] – [Functions are Objects] eechriseg-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 ''function_name''.   * It is given a name by assigning it to a variable -- in this case ''function_name''.
   * 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/tracks/javascript|Codecademy JavaScript course]] and in the definition of event handlers and call-backs to come later.
  
  
 ===== Another Example ===== ===== Another Example =====
  
-  * [[/eg-259/examples/lecture6/medians.html|medians.html]]+  * [[http://jsfiddle.net/cpjobling/vwYGr/3/|medians.html]]
  
 ---- ----
Line 639: Line 638:
  
 <code javascript>  <code javascript> 
-<!DOCTYPE html> +// medians.js 
-<!-- medians.html +// A function and a function tester 
-A function and a function tester +// Illustrates array operations
-Illustrates array operations +
---> +
-<html lang="en"> +
-  <head> +
-    <meta charset="utf-8" /> +
-    <title> Median Computation </title> +
-    <script> +
-      /* Function median +
-       Parameter: An array of numbers +
-       Result: The median of the array +
-       Return value: none +
-       Alternative function definition +
-       */+
  
-      var median = function (list) { 
-        list.sort(function(a, b) { 
-          return a - b; 
-        }); 
-        var list_len = list.length; 
  
-        // Use the modulus operator to determine whether +var element = $('#result');
-        //  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 2)]; +     Function median 
-        } +     Parameter: An array of numbers 
-        else { +     Return valueThe median of the array 
-          return Math.round((list[list_len / 2 - 1] + list[list_len / 2]) / 2); +*/
-        } +
-      }; // end of function median note semicolon +
-    </script> +
-  </head> +
-  <body> +
-    <script> +
-      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("Median of [", my_list_1, "] is", med, "<br />"); +
-      med = median(my_list_2); +
-      document.write("Median of [", my_list_2, "] is: ", med, "<br />"); +
-    </script> +
-  </body> +
-</html> +
-</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'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 / 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("<p>Median of [" + my_list_1 + "] is: " + med + "</p>");
 +
 +med = median(my_list_2);
 +element.append("<p>Median of [" + my_list_2 + "] is: " + med + "</p>");
 +</code>
  
  
Line 703: Line 693:
     myPlane = new Plane("Cessna", "Centurian", "1970");      myPlane = new Plane("Cessna", "Centurian", "1970"); 
 </code> </code>
 +
 +[[http://jsfiddle.net/cpjobling/s8kFw/6/|Fiddle with constructors]].
  
 ---- ----
  
 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) XHTML files that include JavaScript scripts for the following problems. When required to write functions, you must include a script to test the function with at least two different data sets.+Use jsFiddle or some other method to write, test and debug (if necessary) HTML files that include JavaScript scripts for the following problems. When required to write functions, you must include a script to test the function with at least two different data sets.
  
   - //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 ''document.write'' to produce the output.+  - //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 ''console.log'' to produce the output.
   - Modify the script of Exercise 2 to input a number, //n//, using ''prompt'', which is the number of the Fibonacci number required as output.   - Modify the script of Exercise 2 to input a number, //n//, using ''prompt'', which is the number of the Fibonacci number required as output.
   - //Function//: ''no_zeros''; //Parameter//: An array of numbers; //Result//: The array must be modified to remove all zero values. //Returns//: ''true'' if the given array included zeros; ''false'' otherwise.   - //Function//: ''no_zeros''; //Parameter//: An array of numbers; //Result//: The array must be modified to remove all zero values. //Returns//: ''true'' if the given array included zeros; ''false'' otherwise.
eg-259/lecture6.1360269438.txt.gz · Last modified: 2013/02/07 20:37 by eechris