'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;
Solution 1:[1]
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.
Solution 2:[2]
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];
}
Solution 3:[3]
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.
Solution 4:[4]
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
sumvariable, so it will containundefined. When adding to it, the result will continue to beundefined. - The
sumvariable 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,maxandavgvariables 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];
}
Solution 5:[5]
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));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | David Roussel |
| Solution 2 | Vishnu Mishra |
| Solution 3 | ehh |
| Solution 4 | |
| Solution 5 |
