From aab61bdf73cb577f6951eaf4bf921fa97e4d9174 Mon Sep 17 00:00:00 2001 From: ahmedgamal2212 Date: Mon, 1 May 2023 13:02:39 +0300 Subject: [PATCH] Add solution to day 1 --- ...inimum and Maximum Salary (Ahmed Gamal).js | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Gamal).js diff --git a/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Gamal).js b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Gamal).js new file mode 100644 index 000000000..42cd861d6 --- /dev/null +++ b/05- May/01- Average Salary Excluding the Minimum and Maximum Salary/01- Average Salary Excluding the Minimum and Maximum Salary (Ahmed Gamal).js @@ -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; +}; \ No newline at end of file