DEV Community

Pratik sharma
Pratik sharma

Posted on

hackerrank Mini-Max Sum in javascript

function miniMaxSum(arr) {
    // Write your code here
    const sortedArray = arr.sort();

    let min_sum = 0

    let max_sum = 0

    for(let i =0; i < arr.length; i++){
        if( i < arr.length -1 ) {
            min_sum += arr[i]
        } 
        if (i > 0) {
            max_sum += arr[i]
        }
    }

    console.log(min_sum , max_sum) 

}
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

Collapse
frankwisniewski profile image
Frank Wisniewski
const minmax = arr => 
  console.log(`min:${arr.sort().slice(0,-1)
  .reduce((a,b)=>a+b)} max:${arr.sort()
  .slice(1).reduce((a,b)=>a+b)}`)
Enter fullscreen mode Exit fullscreen mode
Collapse
biomathcode profile image
Pratik sharma Author

this.guy().is().doing().functional-programming()