Question:
how would i write a javascript function that?
jon j
2012-06-18 20:07:49 UTC
. Write a function, getAvg, that will accept a variable number of numeric arguments
and return their average.

. Write a function, getMax, that will accept a variable number of numeric arguments
and return the maximum value passed to it.

. Write a function, getMin, that will accept a variable number of numeric arguments
and return the minimum value passed to it.

here is what i have but when i took it to expression it doesnt work function getAvg() {

var avg = 0;

for(var i=0; iavg += arguments[i];
}

return avg / arguments.length;

}
function getMax(vars) {
return Math.max.apply(Math, vars);
}
function getMin(vars) {
return Math.min.apply(Math, vars);
}

function getMax(vars) {
return Math.max.apply(Math, vars);
}
function getMin(vars) {
return Math.min.apply(Math, vars);
}



getAvg(112, 252, 309, 14, 658) =



Four answers:
David
2012-06-20 06:14:48 UTC
Your code doesn't work because you have a syntax error in your in the script tags in the body. This:



document.write(getAvg(112, 252, 309, 14, 658)(getMax)(getMin));



Is not going to return anything because it doesn't make sense. Your script code should all be in the head. In this case there is no need to put it in the body. This is what it should look like:



var array = [112, 252, 309, 14, 658];



document.write( getAvg(array) );

document.write( getMax(array) );

document.write( getMin(array) );



I have rewritten your getAvg code to be able to take the arguments as well as a single array. And I have included getAverage code as well:



getAvg (mean) -- http://jsfiddle.net/SuperBoi45/X2mRE/
anonymous
2012-06-19 01:09:33 UTC
There's a difference between a function which accepts a single parameter which is an array and a function which accepts a variable number of scalar objects. You can pass an array as individual arguments to a function by calling "f.apply", and you can pass a list of a function's arguments to another function as a single array by passing "arguments". Consider the following:



function getMax(lat)

{

        var max = lat[0];



        for (var i = 1; i < lat.length; i++) {

                if (lat[i] > max) {

                        max = lat[i];

                }

        }

        return max;

}



function vgetMax()

{

        getMax(arguments);

}



getMax works with a single parameter, which is an array of some arithmetic type. If vgetMax was already defined independently of getMax, you could implement getMax with the following line:



vgetMax.apply(null, lat);



The goal seems to be to implement Math.min, Math.max, rather than to simply write wrappers around them. You already know how to implement them, so you may as well do it without the use of Math.* functions.



Anyway, as for testing the code, you can do so with some browsers (I use Firefox, I'm not sure if others have this functionality) by typing "javascript:" in the URL bar, then pasting the code after it. You'll have something like:



javascript:document.write("Hello, world");



Then just hit enter and it will execute it.



edit:

function max(a, b)

{

        return a > b ? a : b;

}



function min(a, b)

{

        return a > b ? b : a;

}



function add(a, b)

{

        return a + b;

}



function proc(lat, f)

{

        var n = lat[0];



        for (var i = 1; i < lat.length; i++) {

                n = f(n, lat[i]);

        }

        return n;

}



function getmax()

{

        return proc(arguments, max);

}



function getmin()

{

        return proc(arguments, min);

}



function getaverage()

{

        return proc(arguments, add) / arguments.length;

}
Andy
2012-06-18 20:27:19 UTC
Lol soz i didn't get to reply to your edit, what do you mean how do you write it?

oo right well functions usually go in the head, and you would put document.write() wherever in the body you want it to put the text. like this:



edit: err this prob should work:



















murrell
2016-10-22 06:20:53 UTC
at the start shall we not overlook Java and JavaScript are actually not a similar difficulty. a clean consumer would be extremely at a loss for words in the event that they did not be attentive to the adaptation. i think of it is what your searching for. those seem as though particularly canned questions, are you useful you dont artwork for yahoo?
Loading...