Max/Min/Avg returning array in that order

Can anyone tell me what I'm doing wrong? I am printing the array just not in the right order. How do i call it differently? This is javascript by the way.

Given an array x (e.g. [1,5, 10, -2]), create an algorithm (sets of instructions) that returns an array with the max, min, and average values ([max, min, avg]. ex [0,2,4] should return [4,0,2]).

My code:

function maxMinAvg(arr) {
    var newarr= [];
    var max = arr[0];
    var min = arr[0];
    sum = sum + arr[0];
    var avg = sum/arr.length;
    for (var i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
        if (arr[i] < min) {
            min = arr[i];
        }
        sum = sum + arr[i];
    }
    newarr.push([max[i],min[i],avg[i]]);
}

return newarr;

Upvotes: 2

Views: 8002

Answers (7)

vitaly-t
vitaly-t

Reputation: 25840

A little more elegant solution, via a library:

import {map, pipe} from 'iter-ops';
import {numberStats} from './extras';

const input = [1, 5, 10, -2];

const i = pipe(input, numberStats(), map(a => [a.max, a.min, a.avg]));

console.log(...i); //=> [ 10, -2, 3.5 ]

The above solution is based on iter-ops library, plus custom numberStats operator, from the extras.

Upvotes: 0

Craig  Hicks
Craig Hicks

Reputation: 2528

function maxMinMean(arr: number[]) {
  if (!arr.length) return [NaN,NaN,NaN];
  arr.sort((a,b)=>a-b);
  return [
    arr.slice(-1)[0],
    arr[0],
    arr.reduce((sum, x) => sum + x,0) / arr.length,
  ];
}

If you didn't want to modify the original array

function maxMinMean(arrIn: number[]) {
  if (!arrIn.length) return [NaN,NaN,NaN];
  var arr =[...arrIn].sort((a,b)=>a-b);
  return [
    arr.slice(-1)[0],
    arr[0],
    arr.reduce((sum, x) => sum + x,0) / arr.length,
  ];
}

If you wanted fixed point strings

function maxMinMean(arr: number[]) {
  if (!arr.length) return [NaN,NaN,NaN];
  arr.sort((a,b)=>a-b);
  return [
    arr.slice(-1)[0],
    arr[0],
    arr.reduce((sum, x) => sum + x,0) / arr.length,
  ].map(f=>f.toFixed(2));
}

Upvotes: 0

R1z1a
R1z1a

Reputation: 33

Not sure if this works but you could use the arr.sort() function to sort it and you have your min and max, then just add all values and find mean.

function maxMinMean(arr) {
  arr.sort();
  var sum = 0;
  for (let i = 0; i<arr.length; i++){
    sum = sum + +arr[i];
  }
  return [+arr[length - 1], +arr[0], sum / arr.length];
}

This assumes you are trying to do this with array of numbers (It will also work if the numbers in the array are stored as string) Have not tested it but should work fine.

Upvotes: 2

Guffa
Guffa

Reputation: 700342

Your code is not returning the values in the wrong order, it's returning the original array instead of an array with the max, min and average.

The reason that it looks like the items are in the wron g order, is that you have picked test data that happens to be similar to the result that you expect. If you pick data where the input is not so similar to the output, for example [1,2,3,4,100], you see that you get the same array back.

Just create a new array with the values and return it:

return [ max, min, avg ];

Edit:

With the new code that you posted I see some problems:

  • It's calculating the average before it has the sum of all items. it's just using the first item.
  • It doesn't initialise the sum variable, so it will contain undefined. When adding to it, the result will continue to be undefined.
  • The sum variable is not local, so it could interfer with other code.
  • It's pushing an array into the array, so you get an array or arrays as result instead of an array.
  • It's using the min, max and avg variables as if they were arrays when it puts them in the result.

Fixing that, you get:

function maxMinAvg(arr) {
    var max = arr[0];
    var min = arr[0];
    var sum = arr[0];
    for (var i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
        if (arr[i] < min) {
            min = arr[i];
        }
        sum = sum + arr[i];
    }
    var avg = sum / arr.length;
    return [max, min, avg];
}

Upvotes: 0

Vishnu Mishra
Vishnu Mishra

Reputation: 4029

try this...

function maxMinAvg(arr) {
  var max = arr[0];
  var min = arr[0];
  var sum = 0 ;
 arr.forEach(function(value){
     if(value > max)
       max = value;
     if(value < min)
       min = value;
     sum +=value;
 })
 var avg = sum/arr.length;
return [max,min,avg];

}

Upvotes: 1

Ed Ballot
Ed Ballot

Reputation: 3485

The function should look like this

function maxMinAvg(arr) {
    var max = arr[0];
    var min = arr[0];
    var sum = arr[0]; //changed from original post
    for (var i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
        if (arr[i] < min) {
            min = arr[i];
        }
        sum = sum + arr[i];
    }
    return [max, min, sum/arr.length]; //changed from original post
}

Upvotes: 3

ehh
ehh

Reputation: 3480

You already have the values min, max and avg (you have the sum so you can calculate it). Just a create a new array (not your original arr), add those values (min, max and avg) to it and return it.

Upvotes: 0

Related Questions