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

Top comments (5)

Collapse
 
dylan29jf profile image
Dylan Jay Flores • Edited
function miniMaxSum(arr) {
    // Write your code here
    const max = arr.sort((a,b) => b-a).slice(0,4).reduce((a,b)=> a+b,0)
    const min = arr.sort((a,b) => b-a).reverse().slice(0,4).reduce((a,b)=> a+b,0)

    console.log(min,max)
}
Enter fullscreen mode Exit fullscreen mode
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

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

Collapse
 
homezonic profile image
Akande Joshua

This is great, i also solved this using PHP ( Mini-Max Sum Solution using PHP) incase someone needs that.

Collapse
 
bahamas2023 profile image
Lahcene Ouled Moussa

Nice to meet you great job