No Name

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromNo Name
ToMe
Subjectdesperate for answers
Date3 May 2005 08:01
hey, I be truthful with you I have some questions from a class that I am in. 
I don't understand this at all, I'm struggling with the class and the
teacher is not helping me.  Can you help me out with these problems, I don't
know how to approach them. Please help

Write a function which takes an integer value, j, and an array
as arguments.  If the integer value, j, is greater than zero and
less than the length of the array, report all the sums made
For example, given the following input values:


var skipval = 4;
var array = [1,2,3,4,5,6,7,8,9,10,11,12];
var Sum = skipAdd(skipval,array);

The function should result in:
//Sum[0]= 1+5+9 = 15
//Sum[1]= 2+6+10 = 18
//Sum[2]= 3+7+11 = 21
//Sum[3]= 4+8+12 = 24


If the input was:
var skipval = 2;
var array = [1,1,-2,7,5,12,-6,13,1,3,5,7];
var result = skipAdd(skipval,array);

The function should result in:
// result[0]= 1 + -2 + 5 + -6 + 1 + 5 = 4
// result[1]= 1 + 7 + 12 + 13 + 3 + 7 = 43


Create an function which takes an array 
of floating point values and
returns an object with the following properties:
	sum     - the sum of all the values
	average - the average of the values
	min     - the smallest value
	max     - the largest value

hope to hear from you
FromMe
ToNo Name
SubjectRe: desperate for answers
Date3 May 2005 15:16
I am guessing this is a JavaScript class (since you did not specify). If it
is not JavaScript, ignore my answers, as they apply only to JavaScript. I
will not give you the final answer, but I will give you the information you
need to produce the answer yourself.

> Write a function which takes an integer value, j, and an array
> as arguments.

I am guessing you already know how to write functions. If not, see:
http://www.howtocreate.co.uk/tutorials/javascript/functions

Remember that in JavaScript, we don't care if an argument (parameter) is a
string, integer, array, whatever, just declare a function that accepts two
parameters.

> If the integer value, j, is greater than zero and
> less than the length of the array

The word "if" is the key here. Use the "if" control structure:
http://www.howtocreate.co.uk/tutorials/javascript/controls

Use the greater than operator to check if the number is greater than 0, and
the less than operator to check that the number is less than the length of
the array. ArrayVariable.length gives the length of the array.
http://www.howtocreate.co.uk/tutorials/javascript/operators

The && operator appends the conditions:
if( ( foo > bar ) && ( foo < baz ) )

> report all the sums made

Now things get a bit more difficult. You will neet to use a "for" or "while"
loop. In fact, you will need to use two of them.
say if the integer is 3, you will need to sum:
array[0]+array[3]+array[6]...etc
array[1]+array[4]+array[7]...etc
array[2]+array[5]+array[8]...etc

So you can see that you need to have 'n' sums, where n = the integer (the
integer is 3, I do 3 sums).

var returnVar = new Array();
for( var i = 0; i < theInteger; i++ ) {

this makes it do the correct number of sums. And you will notice that i is
always the first array index needed in the sum (0,1,2).

Now you need to create the sum. You need to step through the array in steps
of 'n', starting at i, until you run out of array entries:
returnVar[i] = 0;
for( var thisCell = i; thisCell < theArray.length; thisCell += theInteger ) {
  returnVar[i] += theArray[thisCell];
}

> Create an function which takes an array of floating point values and
> returns an object with the following properties:
>     sum     - the sum of all the values
>     average - the average of the values
>     min     - the smallest value
>     max     - the largest value

by comparison, this should be easy ;)
Use a for loop to check if each number is bigger or smaller than the last
known biggest/smallest number, and to add them to a total.


Hope this helps

Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.