Next: , Up: (dir)

Scripting Languages G22.3033-002 Summer 2008


Previous: Top, Up: Top

Scripting Languages G22.3033-002 Summer 2008 Example solutions for hw06

Assigned Th 6/26/2008, due Fr 7/4 at 9pm. 50 points.

http://www.cs.nyu.edu/courses/summer08/G22.3033-002/


Next: , Up: solutions-hw06

Instructions for example solutions

These are example solutions. Please keep in mind that often, there is not just one correct solution to a question. If you come up with different answers, then it may be that both your answers and these answers here are correct. Of course, these answers here may also contain mistakes. If you spot a mistake, please let me know so I can correct it.


Previous: Instructions, Up: solutions-hw06

Example solutions for Homework 6


Next: , Up: Solutions-hw06

solutions-hw06-1 Closures

  1. 6, 10
  2. The function is the anonymous inner function nested in curry. The environment is a call object for curry, with f=add and x=1.
  3. The function is the anonymous inner function nested in curry. The environment is a call object for curry, with f bound to the anonymous function function(x,y){x/y} and x=2.
  4.           var square = curry(
                function(n,x){ for(var z=1;n>0;n--,z*=x); return z; },
                2);
         


Next: , Previous: solutions-hw06-1, Up: Solutions-hw06

solutions-hw06-2 Prototypes

  1.           Cf, Dg, Ch
              Cf, Dg, Eh
         
  2. D.prototype, C.prototype, Object.prototype
  3. E.prototype, D.prototype, C.prototype, Object.prototype


Next: , Previous: solutions-hw06-2, Up: Solutions-hw06

solutions-hw06-3 Temperature conversions (JavaScript)

     <html><head>
       <meta http-equiv="Content-Script-Type" content="text/javascript" />
       <script>
         function c2f() {
           var c = document.converter.celsius.value;
           var f = c * 9 / 5 + 32;
           document.converter.fahrenheit.value = f;
         }
         function f2c() {
           var f = document.converter.fahrenheit.value;
           var c = (f - 32) * 5 / 9;
           document.converter.celsius.value = c;
         }
       </script>
     </head><body>
       <form name="converter">
         <table border=0>
           <tr>
             <td>Celsius:
             <td><input type="text" name="celsius">
             <td><input type="button" value="To Fahrenheit" onclick="c2f();">
           <tr>
             <td>Fahrenheit:
             <td><input type="text" name="fahrenheit">
             <td><input type="button" value="To Celsius" onclick="f2c();">
         </table>
       </form>
     </body></html>


Previous: solutions-hw06-3, Up: Solutions-hw06

solutions-hw06-4 Priority queue (JavaScript)

     function PriorityQueue() {
       this.data = [];
     }
     PriorityQueue.prototype.push = function(x) {
       this.data.push(x);
     }
     PriorityQueue.prototype.minIndex = function() {
       var result;
       for (var i in this.data)
         if (undefined == result || this.data[i] < this.data[result])
           result = i;
       return result;
     }
     PriorityQueue.prototype.peek = function() {
       return this.data[this.minIndex()];
     }
     PriorityQueue.prototype.pop = function() {
       var i = this.minIndex();
       var result = this.data[i];
       delete this.data[i];
       return result;
     }