Skip to content

Commit

Permalink
Add solution to day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedGamal2212 committed May 1, 2023
1 parent b51a31e commit aab61bd
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Author: Ahmed Gamal

/**
* @param {number[]} salary
* @return {number}
*/

var average = function(salary) {
let ans = 0;

// sort the array to get the minimum and maximum values in the first and last index, then loop through the array and add all values except the first and last values
salary.sort((a, b) => a - b).forEach((val, idx) => {
if(idx !== 0 && idx !== salary.length - 1) {
ans += val;
}
});

// divide the sum by the length of the array - 2 to get the average
ans /= salary.length - 2;

// return the average
return ans;
};

0 comments on commit aab61bd

Please sign in to comment.