Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is answer NaN?

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading