[an error occurred while processing this directive] File: part2.html Creator: George Ferguson Created: Wed Oct 17 13:52:23 2012 Time-stamp: [an error occurred while processing this directive] File: templates/doc-start.shtml Creator: George Ferguson Created: Tue Dec 6 12:31:29 2011 Time-stamp: [an error occurred while processing this directive] File: site-settings.shtml Creator: George Ferguson Created: Tue Dec 6 13:49:49 2011 Time-stamp: Site (or subsite)-wide settings. [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: head.shtml Creator: George Ferguson Created: Tue Dec 6 12:34:15 2011 Time-stamp: SSI variables for this template: head_title if given, else ``sitename | title'' [an error occurred while processing this directive] File: head-title.shtml Creator: George Ferguson Created: Tue Dec 6 14:29:52 2011 Time-stamp: SSI variables for this template: head_title: complete content of title element if given site_title trailing part of title (if given) section_title middle part of title (if given) page_title leading part of title (if given) title title shown on page, also used as leading part of title (if given) [an error occurred while processing this directive] (none) [an error occurred while processing this directive] File: head-meta.shtml Creator: George Ferguson Created: Tue Dec 6 14:29:18 2011 Time-stamp: SSI variables for this template: meta_description meta_keywords meta_generator [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: head-stylesheets.shtml Creator: George Ferguson Created: Tue Dec 6 14:22:58 2011 Time-stamp: SSI variables for this template: site_stylesheet, page_stylesheet stylesheet0, stylesheet1, ...: URL for stylesheets [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: head-scripts.shtml Creator: George Ferguson Created: Tue Dec 6 14:23:19 2011 Time-stamp: SSI variables for this template: site_script, page_script script0, script1, ...: URLs of javascript scripts [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] File: body-start.shtml Creator: George Ferguson Created: Tue Dec 6 13:20:46 2011 Time-stamp: SSI variables for this template: body_class page_class page_id [an error occurred while processing this directive] [an error occurred while processing this directive] File: body-header.shtml Creator: George Ferguson Created: Tue Dec 6 13:46:12 2011 Time-stamp: Content above banner, if any. [an error occurred while processing this directive] File: body-banner.shtml Creator: George Ferguson Created: Tue Dec 6 13:27:12 2011 Time-stamp: Duplicate URCS Drupal banner. hajim urcs
Computer Science @ Rochester
Computer Science Department : Hajim School of Engineering & Applied Sciences Skip to main content [an error occurred while processing this directive] File: body-title.shtml Creator: George Ferguson Created: Tue Dec 6 14:34:49 2011 Time-stamp: SSI variables for this template: section_name First line of title on page (if any) title Title shown on page

[an error occurred while processing this directive] CSC170: Introduction to Programming and the Web
Fall 2012
[an error occurred while processing this directive]
Assignment 6: Functions, Part 2

  1. [2 pts] Write a script with two global variables: count and total. Write a function with a local variable count that does the following:
    1. Increments count by one;
    2. Asks the user for a number using the current value of count in the prompt;
    3. Adds the number to total;
    4. Adds a line with the current values of count and total to the document.
    The main body of your script should call the function three times. After the first call, it should set count to 10. After the second call, it should set total to 42.

    Warning: Your program's behavior may be strange. You should understand why.

  2. [4 pts] This question was made optional Sun Oct 21. See the BB announcement for further details and discussion.

    In Javascript, the method String.charAt(i) returns the character at the i'th position in a string, starting at 0 for the first character. For example:

          var s = 'Fred';
          document.write(s.charAt(0));
        
    This will produce the single character F (with no quotes). Try it. (Technically it's a string of length one; Javascript doesn't have a character data type.) Similarly, s.charAt(1) would be the single character r, s.charAt(2) would be e and s.charAt(3) would be d. We'll see more about these built-in methods soon, but for now let's just use it to get characters from strings.

    Write a recursive function that accepts a string as its only parameter and returns the string which is the reverse of the original string. For example on input Fred, it would return derF. Illustrate your function on a page that asks the user for a string and displays it and its reverse.

  3. [4 pts] Pawlicki's function, written P(n) in math, is defined as the sum of terms -i(i-1) where i ranges from 1 to n. In mathematical notation, that would be:
    1. Write a Javascript function that computes Pawlicki's function for a given number using iteration (that is, some kind of loop).
    2. Write a Javascript function that computes Pawlicki's function for a given number using recursion.
    Demonstrate both versions using pages that ask the user for a number and display the number and the result of the function. You could also use a single page that calls the two versions of your function and reports the results. In that case, you would need to give the functions different names, of course.

    Note/Hint: You might want to use a pow(x,n) function like we saw in class to do the exponentiation.

Things to think about

  • Don't forget that every function you define should come with a comment that explains its purpose (briefly), its arguments, and its return value.
  • Variable scoping problems are the source of many, many Javascript errors. Be very careful with variable declarations (var statements) to minimize problems.
  • There is a subtle and powerful relationship between iteration and recursion. Many functions can be implemented either way. For what it's worth, recursion is usually more elegant but iteration is usually faster. In fact, in compiled programming languages, the compiler can sometimes replace recursion with iteration automagically. However not all recursive algorithms can be easily implemented using iteration.

[an error occurred while processing this directive] File: doc-finish.shtml Creator: George Ferguson Created: Tue Dec 6 13:46:48 2011 Time-stamp: [an error occurred while processing this directive] File: body-footer.shtml Creator: George Ferguson Created: Tue Dec 6 14:43:56 2011 Time-stamp: Content at bottom of page, if any.

Last update: Monday, 22-Oct-2012 09:41:09 EDT

[an error occurred while processing this directive] File: body-finish.shtml Creator: George Ferguson Created: Tue Dec 6 13:47:36 2011 Time-stamp: [an error occurred while processing this directive]