Advertisement
CLooker

Untitled

Jan 14th, 2018
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than 32 bit integer.)
  2.  
  3. Sample Input
  4. 1 2 3 4 5
  5.  
  6. Sample Output
  7. 10 14*/
  8.  
  9. function miniMaxSum(arr) {
  10.     const maxAndMin = arr.reduce((newArr, n, index) => {        
  11.         return newArr.concat(arr.filter((num, ind) => index !== ind).reduce((total, curr) => total += curr, 0));
  12.     }, []).reduce((maxAndMin, n, index) => {
  13.         index === 0 ? (maxAndMin.max = n, maxAndMin.min = n) : null;
  14.         n > maxAndMin.max ? maxAndMin.max = n : null;
  15.         n < maxAndMin.min ? maxAndMin.min = n : null;
  16.         return maxAndMin;
  17.     }, {min: null, max: null});
  18.     console.log(`${maxAndMin.min} ${maxAndMin.max}`);
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement