 # Average Salary Excluding Minimum and Maximum

Easy
12
68.9% Acceptance

In this lab, you will be implementing a function to calculate the average salary of employees excluding the minimum and maximum salary. You are given an array of unique integers `salary` where `salary[i]` is the salary of the `ith` employee. Your task is to return the average salary of employees excluding the minimum and maximum salary. The answers within `10-5` of the actual answer will be accepted.

Example 1:

``````Input: salary = [4000, 3000, 1000, 2000];
Output: 2500.00000;
Explanation: Minimum salary and maximum salary are 1000 and 4000 respectively.
Average salary excluding minimum and maximum salary is (2000+3000) / 2 = 2500;``````

Example 2:

``````Input: salary = [1000, 2000, 3000];
Output: 2000.00000;
Explanation: Minimum salary and maximum salary are 1000 and 3000 respectively.
Average salary excluding minimum and maximum salary is (2000) / 1 = 2000;``````

Constraints:

• `3 <= salary.length <= 100`
• `1000 <= salary[i] <= 10^6`
• All the integers of `salary` are unique.

## Challenges

1. Export the `average` function.
2. Implement the `average` function to calculate average salary excluding minimum and maximum salary.
3. Test the function with given constraints and examples.

## Evaluation script

``````1import fs from 'fs';
2import { average } from '/home/damner/code/index.js';
3
4const testlog = [];
5
6// this first block matches with - Challenge 1
7try {
8	if (typeof average === 'function') {
9		testlog.push({ status: 'pass' });
10	} else {
11		throw new Error('average function is not exported');
12	}
13} catch (error) {
14	testlog.push({
15		status: 'error',
16		error: error.message || 'Challenge 1 failed',
17	});
18}
19
20// this second block matches with - Challenge 2
21try {
22	const result1 = average([4000, 3000, 1000, 2000]);
23	const result2 = average([1000, 2000, 3000]);
24	const result3 = average([6000, 5000, 4000, 3000, 2000, 1000]);
25
26	if (
27		Math.abs(result1 - 2500) <= 1e-5 &&
28		Math.abs(result2 - 2000) <= 1e-5 &&
29		Math.abs(result3 - 3500) <= 1e-5
30	) {
31		testlog.push({ status: 'pass' });
32	} else {
33		throw new Error('average function not implemented correctly');
34	}
35} catch (error) {
36	testlog.push({
37		status: 'error',
38		error: error.message || 'Challenge 2 failed',
39	});
40}
41
42fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog));
43fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')));``````

## Setting up test environment script

``````1#!/bin/bash
2set -e 1
3
4mkdir -p /home/damner/code/.labtests
5
6cat > /home/damner/code/.labtests/package.json << EOF
7{
8    "type": "module"
9}
10EOF
11
12cd /home/damner/code/.labtests
13mv \$TEST_FILE_NAME ./nodecheck.test.js
14
15# import puppeteer doesn't work without it
17
18node nodecheck.test.js 2>&1 | tee evaluationscript.log``````

## Initial file system for user

``````/**
* @param {number[]} salary
* @return {number}
*/
export function average(salary) {

}``````
``````{
"name": "codedamn-lab",
"type": "module"
}``````
``````tabs: ['index.js']
terminals: ['yarn install']
``````