General Details

Due Date: November 29, 2018 [2018-11-29, Thursday of week 14]

The following guidelines are expected for all homework submissions:

Problems From Chapter 4

  • Ch 4, #22: Explain, in your own words, as precisely as possiblethe behavior of this script:
          var i = 0;
          while( i < 10 ) {
             i += 1;
          alert( "All done" );
  • Ch 4, #26: One way to "speed up" the prime number-testing script is as follows:
    Why does this work? Rewrite the script to use this improved algorithm.
  • pg 126, #1 : Write a for statement that alerts 10, then alerts 9, and so on, until a final alert of 0.
  • pg 126, #2 : Write a little script that computes the value of the product of the integers 1 through 20 (inclusive). Use a for statement.
  • pg 126, #3 : What would happen if, in Figure 4.6, the value of the last object's next property were not null but rather a reference to the first object in the chain?
  • pg 126, #4 : In the multiplication table script, the inner loop uses the variable j to iterate through the columns. What would the script have produced if the programmer had accidentally used the variable i for both the outer and inner loop indices? [Hint: be careful about array bounds.]
  • Problems From Chapter 5

  • Ch 5, #3 : Write a function that returns the average of all of the items in an array. For example, average( [4, 5, 7, 2] ) should return 4.5.
  • Ch 5, #5 : Write a function that accepts two numbers and returns a random number between the two values.
  • Ch 5, #8 : Write a function that returns the number of occurrences of a given character in a string. For example, given the string "Rat-a-tat-tat" and the character "t", your function should return 5.
  • Ch 5, #10: Use the Wolfram Alpha decision engine ( to determine why the original balanceAfter function from this chapter gives troublesome results for negative numbers of compounding periods. Ask Wolfram Alpha to graph the function 1000 * (1 + 0.05/n)10n and explain what you are seeing for negative values of n.
  • Ch 5, #11: What is produced by the sum function from this chapter when given an argument that is not an array? Is this worrisome? Discuss whether or not the function should be rewritten to check for a non-array argument.
  • Ch 5, #15: Test the isPrime function in a shell on the following arguments: undefined, null, false, true, "4", " 2 ", " 2 ", and [19].
  • Problems From Chapter 6

  • Ch 6, #25: Create a web page with at least three levels of nested elements, such that its overall structure looks like this:
    Give each element a click handler that identifies, through an alert dialog, the clicked-on element and whether or not the event is in the capturing or bubbling phase. Open the web page and, before clicking on various elements, predict the sequence of alert messages that you will see. Open the same page and click around using different web browsers as well, and determine which browsers have similar or different event behavior. [Hint: read carefully the section in your text book about bubbling and capturing so you understand what is being asked of your program.]
  • pg 206, #3 : Draw a complete tree diagram for the temperature converter web page in Section 6.1.2.
  • Implementation Exercises

    These programming problems are designed to give you practice at creating algorithmic solutions to some straightforward problems. The intent is not to overload you with tons of homework; rather, it is to help you practice the concepts you have learned in class about breaking the problem down into parts, figuring out the algorithm for each part, writing the whole thing down, doing a desk check, and coding it up and testing it with various inputs. To that end, You may select any two of these three problems. If you would like to do all three, feel free to do so, of course. I'll give you one extra credit point for your correct solution.

    Exercise 1: Expand the tic-tac-toe game from your text book to do a four-by-four game instead of three-by-three. (Note: this sounds a lot harder than it is...) The code begins on page 254 of your book.

    Exercise 2 [part one of two parts]:Ch 7, #22: Write a function that takes in a string and returns the reversal of the string. For example:

          reversal( "" )          // returns ""  (the empty string)
          reversal( "a" )         // returns "a"
          reversal( "string" )    // returns "gnirts"

    Exercise 2 [part two of two parts]Ch 7, #23: Next, build a web interface to the string reversal function. Your web page should have a single input box and a button labeled Reverse. Clicking the button should cause the text in the input box to be replaced with the reverse of its current contents. (Bonus: see how few lines of code you can use to accomplish this task.)

    Exercise 3: Given a total monetary amount and set of coin denominations, determine the smallest number of coins required to make the total amount. For example, given the total amount of $1.23 and the denominations 50, 25, 10, 5, and 1, the answer should be two-50, two-10 and three-one coins. If some denomination is provided which is unusable, such as a negative value, the program should simply say "IMPOSSIBLE" for that denomination. The program should be contained in a web page, in which the user inputs an arbitrary amount of money (expressed as a non-negative, whole number of cents) in one text box, followed by a sequence of coin denominations (distinct positive integers) in the second text box. The page must then display in its footer area the optimal way of making change for the given amount using the given set of denominations. If there is no way to make the change, the program must output the message IMPOSSIBLE.

    Python Programming Problems

    The following problems are from the book "Python for Everyone" by Cay Horstmann and Rance D. Necaise, ISBN 978-1-118-62613-9.

    Exercise 4: (PYTHON PROGRAMMING — text exsample 3.2, page 131) Two circles may intersect at a single point, at two points, or at an unlimited number of points when the two circles are coincident (on top of each other). If the circles do NOT intersect, one circle may be contained entirely within the other, or they may be completely separate. Your task is to write a program in Python that obtains the parameters for two circles and reports whether they intersect. For each circle, the inputs should be the X and Y coordinate (on a Cartesian grid) of the center, and the radius of the circle. The output should be one and only one of the possible configurations listed above, as well as the distance between the two center points. (Hint: the distance can be determined by using the Pythagorean Theorem.)

    Exercise 5: (PYTHON PROGRAMMING — Business P3.35) Calculating the tip when you go to a restaurant is not difficult, but your restaurant wants to suggest a tip according to the service diners receive. Write a program in Python that calculates a tip according to the diner's satisfaction as follows:

    Exercise 6: (PYTHON PROGRAMMING — Science P3.40) Sound level L in units of decibel (dB) is calculated by
    L = 20 log( p / p0 ) where p is the sound pressure of the sound and p0 is a reference pressure of 2 * 10-6. The p values are given in "Pascals", abbreviated by "Pa". The following table gives some sample descriptions of certain sound levels:

    1Threshold of pain130 dB
    2Possible hearing damamge120 dB
    3Jackhammer at 1 meter distance100 dB
    4Traffic on a busy roadway at 10 meters distance90 dB
    5Normal conversation60 dB
    6Calm library30 dB
    7Light leaf rustling sound0 dB

    Write a Python program that reads a value and a unit, either dB or Pa, and then prints the closest description from the table above.

    Exercise 7: (PYTHON PROGRAMMING — Science P3.44) A mass, m of 2 kilograms is attached to the end of a rope of length
    r = 3 meters. The mass is whirled around at high speed. The rope can withstand a maximum tension T of 60 Newtons before it will snap. Write a program that accepts a rotation speed V and determines whether such a speed will cause the rope to break. Use the formula T = mV² / r to calculate.