JavaScript Functions


Slides

Slides Here!


Additional Info


In Class Exercises (ICEs)

Part 1: Pros and Cons of Decomposing a Program Into Functions

Open up the Decomp_Example_1.html file (this is the same file that your teacher just showed you, as an example of program decomposition).

Also open the file Non_Decomp_Example_1.html - this does the same thing as the example file except that it puts all the logic into the click handler (i.e., it does not use the three functions that were explained in lecture). This is provided to you so that you can better compare and contrast what a program might look like with, and without, functions.

Your goal is to look over the two programs and come up with a list of pros and cons for decomposing the program into smaller functions.  Please work in groups of 2 to 4 people for this - since it's more of a discussion/brainstorming exercise having more people will be beneficial.

Be prepared to share your pros and cons with the class.

Part 2: Decompose A Program Into Functions

Open up the file Decompose_This_1.html and work in groups of 2 (or at most 3) people to decompose the program into smaller functions.

On the one hand this exercise should be very similar to some of the examples so you should be able to leverage your knowledge of how the instructor split up those programs in order to decompose this one.  On the other hand there's (at least) one new twist in the program that will require a slight change in how things are set up.

Part 3: Decompose A More Complicated Program Into Functions

Open up the file Decompose_This_2.html and work in groups of 2 (or at most 3) people to decompose the program into smaller functions.

I suggest that you start by opening up the file in a browser and playing around with it for a bit - read what's on the page, skim the links about palindromes that the page links to, then try typing in some numbers and see what the results are, then examine the code yourself.

One approach would be to put comments into the code so that you could leave yourself notes about what the different lines of code are doing.  From there you might try 'rolling up' lines into blocks/blobs of logic, and see if the program can be decomposed into a couple of different functions.

Be prepared to share your work with the class.