Average Salary Excluding Minimum and Maximum

Easy
15
69.3% 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

import fs from 'fs'; import { average } from '/home/damner/code/index.js'; const testlog = []; // this first block matches with - Challenge 1 try { if (typeof average === 'function') { testlog.push({ status: 'pass' }); } else { throw new Error('average function is not exported'); } } catch (error) { testlog.push({ status: 'error', error: error.message || 'Challenge 1 failed', }); } // this second block matches with - Challenge 2 try { const result1 = average([4000, 3000, 1000, 2000]); const result2 = average([1000, 2000, 3000]); const result3 = average([6000, 5000, 4000, 3000, 2000, 1000]); if ( Math.abs(result1 - 2500) <= 1e-5 && Math.abs(result2 - 2000) <= 1e-5 && Math.abs(result3 - 3500) <= 1e-5 ) { testlog.push({ status: 'pass' }); } else { throw new Error('average function not implemented correctly'); } } catch (error) { testlog.push({ status: 'error', error: error.message || 'Challenge 2 failed', }); } fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)); fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')));

Setting up test environment script

#!/bin/bash set -e 1 mkdir -p /home/damner/code/.labtests cat > /home/damner/code/.labtests/package.json << EOF { "type": "module" } EOF cd /home/damner/code/.labtests mv $TEST_FILE_NAME ./nodecheck.test.js # import puppeteer doesn't work without it npm link puppeteer node 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']