Lesson 7 stressed the importance of abstraction in programming and problem solving in general. By defining functions, the programmer is able to add new abstractions to the language. Once defined, a function can be used repeatedly without having to worry about the details of its computation. In addition, functions minimize the complexity of code. As opposed to duplicating the code each time a particular computation is required, that code can be written once, encapsulated inside a function, and then executed repeatedly via function calls.
As an example, a file named random.js has been provided for you on the class server. This file contains the definitions of several useful functions involving random number generation and selection. The headers for these functions are listed below.
All of the exercises in this lesson involve using the functions defined in the random.js library. Therefore, you should list the file name random.js in the library box, and then access these functions in your code as needed.
In order to test the functions in the random.js library, evaluate each of the following function calls 10 times and list the results. For each function called, were the results evenly spread over the range of potential values?
Functions defined in library files are no different from functions defined directly in the interpreter. In fact, you can think of the code from libraries as being automatically inserted at the top of the interpreter window each time the "Execute Code" button is clicked. Even though you cannot see the actual definition when it is loaded, your code can access the functions from a library and build upon them.
As an example, consider the diceRoll function that you wrote in Lesson 7.
Once you have cut-and-pasted this function definition into the interpreter, you can call it anytime you want to generate a random sequence of letters. As an example of where this might prove useful, consider the opening lines of Lewis Carroll's poem Jabberwocky.
In the randomSeq function above, the value in the return statement is rather complex, containing four different calls to the randomChar function. You might be tempted to try to simplify this code by calling the function once, saving the result in a variable, and then using that variable in the return expression. Thus, the function would look like this:
A slot machine is a rather simple gambling/entertainment device. It is a box with three (or more) wheels that spin independently when the player pulls a lever. The wheels have symbols printed on them (say a cherry, an orange, and a bar), and if they all stop spinning with identical symbols lined up, then the player wins. For example, three cherries is a winner, while two cherries and an orange is not.
Hint: Each wheel can be simulated using the randomItem function to select a word from the list ["cherry", "orange", "bar"]. The value returned by the slots function can be obtained by concatenating three such random words together (with spaces in between).
Note that with the slot machine described in the previous exercise, there are 3 different combinations that are winners, namely "cherry cherry cherry", "orange orange orange", and "bar bar bar". Since there are 27 possible combinations (3 symbols * 3 symbols * 3 symbols), there is a 1 in 9 chance of winning.
Call your slots functions repeatedly and count how many times it takes to generate a winner. Is it close to 9?
Now call the slots function 36 times and keep track of how many winners you generate. Is it close to 4?