Chapter 3 Exercise Set 2

animal

Create an object named, animal with properties type set to "tiger", gender set to "female", age set to 5, and hungry set to true.

addToCounts(thing)

Create an object named counts that will hold the “count” (i.e. the number of occurances) of things added to it by addToCounts below.

Then write a function named addToCounts(thing) which tracks inside counts the number of times thing is sent to it.

The following sequence of addToCount calls starting with an empty count

addToCounts('a');
addToCounts(7);
addToCounts('b');
addToCounts(7);
addToCounts('b');

should result in a counts object with the following state (when viewed in Firebug):

Object{a=1, 7=2, b=2}

max(nums)

Write a function named max that takes an array of numbers, nums, as an argument and returns the largest number found in the array.

min(nums)

Write a function named min that takes an array of numbers, nums, as an argument and returns the smallest number found in the array.

mean(nums)

Write a function named mean that takes an array of numbers, nums, as an argument and returns the arithmetic mean (the “average”) of the numbers.

sort(nums)

Write a function named sort that takes an array of numbers, nums, as an argument and returns an array of the same numbers sorted from least to greatest.

median(nums)

Write a function named median that takes an array of numbers, nums, as an argument and returns the median of the set of numbers.

Hint

Use the sort function you wrote in the previous exercise to put the numbers in order before trying to computer the median.

mode(nums)

Write a function named mode that takes an array of numbers, nums, as an argument and returns the mode of the set of numbers.

Hint

Use the addToCounts function you wrote in addToCounts(thing) to create a counts object that keeps track of the frequency of each number in nums. Then write another helper function to find the maximum frequency.