Scripting Languages CS5142, Fall 2013 hw08


Concept Questions

hw08_1 Server-side JavaScript

1a.
(3 points) Why might a developer want to run JavaScript on the server side?
1b.
(3 points) How are modules supported in Node? How does this differ from Perl modules?
1c.
(4 points) How does the event-based model of Node differ from web-servers like Apache?

Programming exercises

hw08_2 Complete Web Application

(20 points) Build a client and server for your priority queue.
2a.
(10 points) Write a server using JavaScript and node around your priority queue. The server should handle requests for insert and extract. You may use the sample priority queue code provided in previous homework solutions. You may use the connect and express modules that we discussed in class.
2b.
(2 points) Write a an HTML page with a form that provides a client-side front end to your priority queue. The form should have buttons for insert and extract, and a text box where you enter a number to insert.
2c.
(8 points) Add client-side scripts using JQuery to the HTML page which will contact the server with either an insert of extract. For an insert, the number in the text box should be inserted into the queue on the server side. On an extract, the client should contact the server to get the max element, and display it on the page.
Here is an example, although you are free to use your own design:

Insert 5 into the queue:

Extract max from the queue:

hw10_1 Block parameters in Ruby

Consider the following Ruby script.
#!/usr/bin/env ruby
class Tree
  attr_accessor :value, :left, :right
  def initialize(value, *leftRight)
    @value = value
    if 0 < leftRight.length()
      @left = leftRight[0]
      @right = leftRight[1]
    end
  end
  def preorder()
    yield @value
    if @left
      @left.preorder {|v| yield v }
    end
    if @right
      @right.preorder {|v| yield v }
    end
  end
end

$my_tree = Tree.new(4,
             Tree.new(1, Tree.new(7), Tree.new(3)),
             Tree.new(5, Tree.new(6), Tree.new(2)))

def print_preorder(tree)
  puts 'preorder:'
  tree.preorder {|y| puts y.to_s + "\n" }
end

print_preorder($my_tree)
3a.
(4 points) Predict what the code should print. Then run it. What does it print?
3b.
(6 points) Add another top-level function print_max that uses the preorder method to compute the maximum of all values in the tree. For example, the following call:
print_max($my_tree)
should cause the following output to be printed:
max:
7

http://www.cs.cornell.edu/Courses/cs5142/2013fa/hw08.html