开发者

Get the value from an Array

I have some code I need help with:

var bob = [10,30,50,20,8];

// Call the function

foo(bob);

var foo = function(bob){

 var max = 0,
 min = 0,
 average = 0,
 sum = 0;

 max = Math.max.apply( null, bob); // Get the max value.
 min = Math.min.apply( null, bob); // Get the min value.


 for( var i = 0; i < bob.lengt开发者_如何转开发h; i++ ){
  sum += bob[ i ];
 }

 average = sum / l;

 var resultArr = [];

 resultArr[0] = average;
 resultArr[1] = min;
 resultArr[2] = max;

 return resultArr;
}


document.getElementById('max').innerHTML = "Max value: "; // i want to print the max value here.

This function returns an array with max, min and average values. How can I choose which value to print in different divs?


You already have the answer:

var resultArr = foo(bob);
document.getElementById('max').innerHTML = resultArr[2];
document.getElementById('min').innerHTML = resultArr[1];
// etc


document.getElementById('max').innerHTML = resultArr[2];

Or

<div id="max">
    <script>document.write(resultArr[2]);</script>
</div>

Or (JQuery)

$('#max').val(resultArr[2]);


You have a number of things confused here. You have a global variable bob and you also have bob as a function argument. Also, you have an array of results, which local to the foo function is called resultArr, but you do not assign it anywhere when you call the foo function.

Try the following code instead:

var bob = [10,30,50,20,8];

var foo = function(arrayOfNumbers){
  var resultObj = {
    max: 0,
    min: 0,
    average: 0,
    sum: 0
  };

  resultObj.max = Math.max.apply( null, arrayOfNumbers); // Get the max value.
  resultObj.min = Math.min.apply( null, arrayOfNumbers); // Get the min value.

  for( var i = 0; i < arrayOfNumbers.length; i++ ){
    resultObj.sum += arrayOfNumbers[ i ];
  }

  resultObj.average = sum / l;

  return resultObj;
}

// Call the function

var theResult = foo(bob);

document.getElementById('max').innerHTML = "Max value: "+theResult.max;

Here's a description of the changes I made to your code, and why:

  1. I made sure that the names of your variables don't conflict. This should help you see where each variable is used and how they relate to each other. bob contains your numbers. You pass bob to the foo() function. Inside foo, the numbers are now referred to as v. This means you could also have frank, joe, and tom, and you could call foo on all of them. arrayOfNumbers is a local variable to foo.

  2. I changed resultArr from an array to an object. An object makes more sense because you can assign labels to the values. So now the max value is can be accessed at .max instead of [0]. I also showed how the result object is called resultObj inside foo but gets assigned to theResult outside foo. You can then refer to theResult.min, theResult.max, and theResult.average.

  3. I cleaned up a little bit, fixing indentation and moving the function to the top, to increase readability.

I hope this helps you see what's going on here!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜