0

I'm trying to calculate a function that will return the average for an number of integer arguments. Who can help?

var Calculator = {
  average: function(x) {
    var num = 0;
    for (var i = 1; i <= x.length; i++) 
    num = num + x[i];
    var divide = num/x.length;
    return divide;
 }
};

When I run this code, I get NaN.

Here is the fiddle: http://jsfiddle.net/marcusdei/hsvr8va5/6/

--------------------> Update: Not a duplicate question or answer

Past answers do not support long floats.

Marco V
  • 2,553
  • 8
  • 36
  • 58
  • possible duplicate of [JavaScript variable number of arguments to function](http://stackoverflow.com/questions/2141520/javascript-variable-number-of-arguments-to-function) – Aziz Saleh Jun 20 '15 at 14:48
  • Not a duplicate question or answer. Past answers do not support long floats. – Marco V Jun 20 '15 at 15:31

5 Answers5

1

There's also issues with your iterator. you're overrunning the end of the array, which is NaN.

This makes it run correctly:

for (var i = 0; i < x.length; i++){
        num = num + x[i];      
      }

Here, I updated your fiddle: http://jsfiddle.net/hsvr8va5/10/

Happy to answer specific questions you have about the issues of the iterator.

Thomas Murphy
  • 1,360
  • 4
  • 14
  • 41
1

You can use JavaScript's arguments inside a function to handle arbitrary number of parameters.

 
 var Calculator = {
  average: function() {
    var num = 0,length=arguments.length;
    if(!length) return 0;
    for (var i = 0; i < length; i++) 
            num = num + arguments[i];
    var divide = num/length;
    return divide;
 }
};

document.getElementById('choice').innerHTML = Calculator.average(3,4,5);
    <div id="choice"></div>

<div id="hidden">

</div>

Updated fiddle : http://jsfiddle.net/vikashvverma/hsvr8va5/11/

Vikash
  • 1,109
  • 1
  • 12
  • 19
0

X is expected to be an array. If you want to support variable number of arguments, you need to iterate over "arguments". It's a magical array containing all of the arguments the function was called with.

Also, array indexes start at 0 and end at length-1. Your version skipped the first number at the start, and went out of bounds at the end. This resulted in last addition being num = num + undefined = NaN

function() {
    var num = 0,
        x = arguments;
    for (var i = 0; i < x.length; i++) 
    num = num + x[i];
    var divide = num/x.length;
    return divide;
 }

Fiddle: http://jsfiddle.net/hsvr8va5/12/

Juho
  • 427
  • 2
  • 12
0

Your loop uses incorrect start value and end condition. Also, the function must be called with an array as argument. The code should look like this:

var Calculator = {
  average: function(x) {
    var num = 0;
    for (var i = 0; i < x.length; i++) 
        num = num + x[i];
    var divide = num/x.length;
    return divide;
 }
};

document.getElementById('choice').innerHTML = Calculator.average([3,4,5]);
Petter
  • 4,053
  • 27
  • 33
0

your code is fine, except the part where you iterate in your for loop, here's your code

var Calculator = {
  average: function(x) {
    var num = 0;
    for (var i = 0; i < x.length; i++) 
    num = num + x[i];
    var divide = num/x.length;
    return divide;
 }
};

you have to watch out for Loop Invariants. Loops are subject to the banana problem (I know how to spell, but I don’t know when to stop). JavaScript is zero indexed!

Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47