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;
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;
Share
Improve this question
edited Jul 4, 2015 at 20:51
Antwane
22.7k8 gold badges56 silver badges89 bronze badges
asked Jul 4, 2015 at 17:39
Theodore Rollin AndersonTheodore Rollin Anderson
851 silver badge9 bronze badges
3
- Why are you returning the array without modifying it? – Delgan Commented Jul 4, 2015 at 17:41
-
You're returning the original array.
return [max, min, avg];
– user1106925 Commented Jul 4, 2015 at 17:41 - how do i return it in the max,min, avg order? – Theodore Rollin Anderson Commented Jul 4, 2015 at 17:43
7 Answers
Reset to default 3The 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
}
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.
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];
}
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.
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 containundefined
. When adding to it, the result will continue to beundefined
. - 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
andavg
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];
}
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));
}
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.