Home > Net >  Find a set of numbers inside in array
Find a set of numbers inside in array

Time:07-07

function BigNumebr(e) {
    let num = ["19", "25" , "30" , "13", "22"]
    if ()
}

I want to get the biggest number, middle number and small number use an array without any method, :)

CodePudding user response:

You could use sort() to sort the array, then pick the first, middle and last element from the sorted array.

let num = ["19", "25" , "30" , "13", "22"];

const sorted = num.sort((a, b) => a - b);
const result = [sorted[0], sorted[Math.round((sorted.length - 1) / 2)], sorted[sorted.length-1]];

console.log(result);

CodePudding user response:

Use Array.sort and then pick first, middle, last numbers.

  • Related