1
 var avg = function() 
   {
     var sum = 0;
      for (var i = 0, j = arguments.length; i < j; i++) 
        {

           sum += arguments[i];
         }
      return sum / arguments.length;
   }

When I try to call this like:

var average = avg(2,3,5);
average; // It works fine;

But how do I call it without assigning to a variable? If anybody can give any suggestion it will be delightful..Thanks.

1
  • Are you experimenting in a JavaScript console and want to see the result? Commented Sep 15, 2012 at 12:20

4 Answers 4

2

You'd simply call it like this:

avg(2, 3, 5);

If you want to see the result, put it in an alert call:

alert( avg(2, 3, 5) );
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, for your Suggestion . Can You tell me how to Use this function like a javascript Closures.
I'm sorry. What exactly do you mean by "javascript closures"?
function makeAdder(a) { return function(b) { return a + b; } } x = makeAdder(5); y = makeAdder(20); x(6);// I mean this Javascript closures , I don't know exactly what it's name.Maybe I wrong!!!
You could call this "hardcoded currying", i.e. partial application of parameters. The second snippet allows you to curry any function.
@HumayounKabir I see what you mean. You'd do it like this: jsfiddle.net/MAqP3
|
0

You don't need to put the result from calling the function in a variable, you can do whatever you like with it.

For example, use it as the value in an alert:

alert(avg(2,3,5));

You can use it in another expression:

var message = "The average is " + avg(2,3,5);

You can use it directly in another call:

someFunction(avg(2,3,5));

You can even throw the result away by not doing anything with it, even if that's not useful in this specific situation:

avg(2,3,5);

Comments

0

If you don't put the result into a variable or in a compatible context, this function cannot output anything, which makes it difficult to use unless you make it output the result. Try this :

var avg = function() 
   {
     var sum = 0;
      for (var i = 0, j = arguments.length; i < j; i++) 
        {

           sum += arguments[i];
         }

      var retvalue = sum / arguments.length;
      consoloe.log("avg: "+retvalue);
      return retvalue ;
   }

Then it may help you to see whenever the function is called or not.

Comments

0

You need to understand the concept of expressions.

Every expression as a whole represents one value. An expression can be made up of multiple subexpressions that are combined in some manner (for example with operators) to yield a new value.

For instance:

  • 3 is an expression (a literal, to be specific) that denotes the numeric value three.
  • 3 + 4 is an expression, made up of two literal expressions, that as a whole yields the value 7

When you assign a value to a variable – as in var average = – the right hand side of the =-operator needs to be an expression, i.e. something that yields a value.

As you have observed, average will have been assigned the value five. It thus follows, that avg(2, 3, 5) must itself be an expression that evaluated to the value 5.

average itself is an expression, denoting the current value of said variable.

The most important thing to take away from this is, that avg() is in no way connected to var average =. avg() stands on its own, you can just think it like an ordinary value such as 5 (there are other differences of course).

If you have understood the concept of expressions and values, it should be clear that if you can do

var average = avg(2,3,5);
average; // It works fine;

You can also do

avg(2,3,5);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.