DEV Community

Discussion on: What’s your alternative solution? Challenge #52

Collapse
 
bherrero profile image
Borja Herrero

Nice one! I'd go with this one.

const ar = [2, 4, 10, [12, 4, [100, 99], 4], [3, 2, 99], 0];

const max = findMax(ar);
println("Max  = ", max);

function findMax(array) {
    // First we flatten the array using Infinity as depth
    // Then we sort the numbers from higher to lower
    const sortedFlatArray = array.flat(Infinity).sort((a, b) => b - a);

    // First item it's the highest
    return sortedFlatArray[0];
}