SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up calculating the average/mean

    Hi,

    Im trying to calculate the mean/average of numbers in an array any suggestions with the mean() would be very much appreciated.

    how can i get the length of an array to perform in a simple / maths problem whilst being flexiable to the length of the array?

    function sum()
    {
    var i=0
    for (i=0, total=0;i <numbers.length; i++)
    {
    total += numbers[i];
    }
    document.getElementById("txtTotal").value = total;
    }



    function mean()
    {

    document.getElementById("answer") = parseFloat(document.getElementById("txtTotal").value) / parseFloat(numbers.length);
    }

  2. #2
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Try this.
    Code JavaScript:
     var numberArray=[1,2,3,4,5], thisTotal=0,thisAverage=0;
    // add elements of array together 
     for(var i=0;i<numberArray.length;i++)
      {thisTotal+=numberArray[i];}
    // calculate average  
     thisAverage=(thisTotal/numberArray.length);
    // display result 
     alert(thisAverage)

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    I've whipped up some functions that calculate mean, median, mode, and range.

    Code javascript:
     
    function mean(numbers) {
        // mean of [3, 5, 4, 4, 1, 1, 2, 3] is 2.875
        var total = 0,
            i;
        for (i = 0; i < numbers.length; i += 1) {
            total += numbers[i];
        }
        return total / numbers.length;
    }
    function median(numbers) {
        // median of [3, 5, 4, 4, 1, 1, 2, 3] = 3
        var median = 0,
            numsLen = numbers.length;
        numbers.sort();
        if (numsLen % 2 === 0) { // is even
            // average of two middle numbers
            median = (numbers[numsLen / 2 - 1] + numbers[numsLen / 2]) / 2;
        } else { // is odd
            // middle number only
            median = numbers[(numsLen - 1) / 2];
        }
        return median;
    }
    function mode(numbers) {
        // as result can be bimodal or multimodal,
        // the returned result is provided as an array
        // mode of [3, 5, 4, 4, 1, 1, 2, 3] = [1, 3, 4]
        var modes = [],
            count = [],
            i,
            number,
            maxIndex = 0;
        for (i = 0; i < numbers.length; i += 1) {
            number = numbers[i];
            count[number] = (count[number] || 0) + 1;
            if (count[number] > maxIndex) {
                maxIndex = count[number];
            }
        }
        for (i in count) if (count.hasOwnProperty(i)) {
            if (count[i] === maxIndex) {
                modes.push(Number(i));
            }
        }
        return modes;
    }
    function range(numbers) {
        // range of [3, 5, 4, 4, 1, 1, 2, 3] is [1, 5]
        numbers.sort();
        return [numbers[0], numbers[numbers.length - 1]];
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •