generate functionMon, 20 Feb 2023

Return the "centered" average of an array of ints, which we'll say is the mean average of the values, except ignoring the largest and smallest values in the array. If there are multiple copies of the smallest value, ignore just one copy, and likewise for the largest value. Use int division to produce the final average. You may assume that the array is length 3 or more.

var centered_average = function(nums) { var sum = 0; var max = nums[0]; var min = nums[0]; for (var i = 0; i < nums.length; i++) { if (nums[i] > max) { max = nums[i]; } if (nums[i] < min) { min = nums[i]; } sum += nums[i]; } return (sum - max - min)/(nums.length-2); };

Generate More

Questions about programming?Chat with your personal AI assistant