CS 74.42A Game Programming - Lab 2 (Functions) - by Jeff Moritz

Num Question Display
Code
Run
Code
1. Write a function that converts fahrenheit to celcius. Show Run
2. Write a function that converts degrees to radians. (Hint: In javascript, Pi is defined as Math.PI) Show Run
3. Write a function that converts radians to degrees. Show Run
4. Write a function that simulates a coin toss, returning true for heads and false for tails. Show Run
5. Use closures to write a function that generates random dice rolls for the following types of dice: d4, d6, d8, d10, d12, d20. Show Run
6. Write a function that takes as input two numbers and returns the greater of the two numbers. If the inputs are equal, just return the number. Show Run
7. Write a function that takes as input two numbers and returns the lesser of the two numbers. If the inputs are equal, just return the number. Show Run
8. Write a function that takes two integers as arguments, and returns a sum of all the numbers between those two numbers (including the two argument numbers). So if the function is passed 1 and 5, it will return the number value 15 (i.e., 1 + 2 + 3 + 4 + 5). Assume the first argument is always less than the second argument. Show Run
9. Write a function that behaves like the function in question 8, but handles two special cases: if the arguments are equal, return that number. If the first argument is greater than the second, it still produces the correct sum (so if you named this function "sum", then sum(1, 5) and sum(5, 1) would both return 15 (1 + 2 + 3 + 4 + 5). Show Run
10. Modify the random integer function given in class so that it returns a valid result even if the arguments to the function are reversed (i.e., the first argument is greater than the second). Show Run
11. Write two functions such that, if either function is called, the javascript interpreter stack will overflow. (Hint: An example of this can be found in chapter 3). Make sure the test for this function is the last test run in your program! Show Run
12. Write a game of guess the number that behaves like the demonstration given in class:

The program starts with an alert like "I am thinking of a number between 1 and 100. Can you guess what number I'm thinking of?" The user is then prompted to enter a number. If the entered number is lower than the number the program has picked, a new alert says "Guess higher!" and prompts the user to enter another guess. If the entered number is higher than the picked number, it says "Guess lower!" This continues until the user guess the correct number. After the user has picked the correct number, another prompt appears: "You guessed the number! Would you like to play again?" If the user enters "yes" then play another round of the game. Otherwise, the program ends.
Show Run