Home > OS >  finding the max/min sum in js... why does this fail?
finding the max/min sum in js... why does this fail?

Time:05-10

function miniMaxSum(arr) {
  // Write your code here
  let max = Math.max(...arr);
  let min = Math.min(...arr);
  let minsum = 0;
  let maxsum = 0;
  for (let x in arr) {
    if (arr[x] != max) {
      minsum  = arr[x];
    };
    if (arr[x] != min) {
      maxsum  = arr[x];
    }
  };
  console.log(minsum, maxsum);
}

I got this from hackerrank, and apparently it fails certain test cases but I have to pay 5 "hackos" to learn why

CodePudding user response:

Are all the integers supposed to be unique? If not then this may not work if there are duplicates of max and min numbers?

For example [2,2,3,5,5]

CodePudding user response:

So, I just got the problem to understand what it is supposed to do. What you need to do is to find the 4 biggest values in an array of integers and sum it, and also need to find the 4 smallest values and sum it. (hackerrank link: https://www.hackerrank.com/challenges/mini-max-sum/problem)

I'll let the code down below with comments so you can understand

function miniMaxSum(arr) {
    //make a copy of the original array to calculate the max sum value
    let arrMax = [...arr];
    //make a copy of the original array to calculate the min sum value
    let arrMin = [...arr];
    let maxSum = 0;
    let minSum = 0;

    // We need to find the sum of the biggest/lowest 4 values
    for (let i = 0; i < 4; i  ) {

        //find the index of the element with the biggest value
        let maxElementIndex = arrMax.findIndex(value => value === Math.max(...arrMax));
        //sum the value to the max sum result
        maxSum  = arrMax[maxElementIndex];
        //remove the value from the array
        arrMax.splice(maxElementIndex,1);

        // here I am doing the same, but now to get the lowest value
        let minElementIndex = arrMin.findIndex(value => value === Math.min(...arrMin));
        minSum  = arrMin[minElementIndex];
        arrMin.splice(minElementIndex,1);
    }
    console.log(`${minSum} ${maxSum}`);
}

Now, some tips for you before you create a new question.

  1. create with the max of details, for example, you could explain better the problem before sending the code.
  2. Try to explain what you understood for the problem and what your are trying to do. I believe you just misunderstood what the problem was asking.

I hope I could help you. You can leave any comment if something is difficult to understand and I will try to help you. And happy coding! :)

CodePudding user response:

try this

 let n = prompt('Enter your number:');
let number = [];
for (i = 0; i < n; i  ) {
    number.push(Number(prompt('Enter #'   i   'number:')));
}
let sum = 0;
const max = Math.max.apply(null, number);
const min = Math.min.apply(null, number);
for (i = 0; i < n; i  ) {
    sum  = number[i]
}
let average = sum / n;
console.log(`max is ${max}`);
console.log(`min is ${min}`);
console.log(`average is ${average}`);
  • Related