Missing page.
Sorry. The page you requested does not exist on the Department of Computer Science website
Return to the Computer Science Home Page
[an error occurred while processing this directive]
File: body-title.shtml
Creator: George Ferguson
Created: Tue Dec 6 14:34:49 2011
Time-stamp:
SSI variables for this template:
section_name First line of title on page (if any)
title Title shown on page
[an error occurred while processing this directive]
CSC170: Introduction to Programming and the Web
Fall 2012
[an error occurred while processing this directive]
Assignment 7: Arrays and Builtins: Part 1
- [4 pts]
Write your own version of the Javascript Array method
indexOf. That is, write a function that takes as
parameters an array and a value and returns the index of the first
element that is equal to the value, or -1 if the value is not one
of the elements in the array.
Demonstrate your function on a page that asks the user for a
color name and returns the index of that color in an array
containing the
17 predefined CSS color names (in row order top-to-bottom, then
left-to-right within a row).
- [4 pts]
Write a method that returns a copy of given array. That is, your
function should take one parameter, an array, and return an array
with exactly the same elements in it (but not the same array
itself). That is the following is not correct:
function copyArray(a) {
return a;
}
- [4 pts]
Write a function that computes and returns the average (mean) of
an array of numeric values. Demonstrate your function on a page
using the following values:
59 74 76 26 86 89 21 74 39 83
- [4 pts]
Write a function that computes the maximum and minimum values in
an array of numbers, and returns both values as an array in that order.
Demonstrate your function on a page using the following values:
89 -35 11 -14 -31 -40 73 -40 -29 -81
Things to think about
-
References to elements of an array (e.g.,
a[i]) can
be used in expressions like any other (scalar) variable.
-
Array-valued variables can be passed to functions like any other
value. The function parameter has local scope, but the contents of
the array (that is, its elements) can be changed within the
function.
-
Loops (iteration) and arrays go great together.
-
Many things you do in a program can be bundled up into small
functions. Not only do these make it easy to understand your
program, they can also be used in other programs.
On to Part 2 when it is available...
[an error occurred while processing this directive]
File: doc-finish.shtml
Creator: George Ferguson
Created: Tue Dec 6 13:46:48 2011
Time-stamp:
[an error occurred while processing this directive]
File: body-footer.shtml
Creator: George Ferguson
Created: Tue Dec 6 14:43:56 2011
Time-stamp:
Content at bottom of page, if any.
Last update: Tuesday, 23-Oct-2012 14:52:41 EDT
[an error occurred while processing this directive]
File: body-finish.shtml
Creator: George Ferguson
Created: Tue Dec 6 13:47:36 2011
Time-stamp:
[an error occurred while processing this directive]