Home > Software design >  Why is answer NaN?
Why is answer NaN?

Time:10-11

I have five positive integers, I want to find the minimum and maximum values that can be calculated by summing exactly four of the five integers. index of array is number, but when I print answer, it's NaN

var sumArr = []
var sum = 0
function miniMaxSum(arr) {
    // Write your code here
    for(let i = 0; i < arr.length; i  ){
        
        sumArr.push(arr.filter(item => item !== arr[i]).reduce((previousValue, currentValue) => previousValue   currentValue, sum))
    }

    console.log(Math.min(sumArr), Math.max(sumArr))
}

let sides = [1, 2, 3, 4, 5];


const result = miniMaxSum(sides);

CodePudding user response:

Because you're passing an array, those functions are expecting numbers instead. Take a look at Math

You can spread those arrays as follows:

var sumArr = []
var sum = 0
function miniMaxSum(arr) {
    // Write your code here
    for(let i = 0; i < arr.length; i  ){
        
        sumArr.push(arr.filter(item => item !== arr[i]).reduce((previousValue, currentValue) => previousValue   currentValue, sum))
    }

    console.log(Math.min(...sumArr), Math.max(...sumArr))
}

let sides = [1, 2, 3, 4, 5];


const result = miniMaxSum(sides);

  • Related