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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay