DEV Community

Pratik sharma
Pratik sharma

Posted on

9 2 1

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
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

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more