DEV Community

Discussion on: Explain This Like I'm Five

Collapse
 
alephnaught2tog profile image
Max Cerrina

And for good measure, what I had thought they wanted and how I would've written it:

function getMaxOfAllArrays(groupOfArrays) {
        var largestNumberArray = [];
        var largestNumber = 0; // picking 0 to start

        // loop through our arrays
        for (var whichArray = 0; whichArray < groupOfArrays.length; whichArray++) {
            var currentArray = groupOfArrays[whichArray];

            // index change -- since we are no longer comparing within the array
            // we need to loop through the whole thing.
            for (var whichNumber = 0; whichNumber < currentArray.length; whichNumber++) {

                var currentNumber = currentArray[whichNumber];

                if (currentNumber > largestNumber) {
                    largestNumber = currentNumber;
                    // we can't stop here--what about the other arrays?
                }
            }
        }

        // return the max
        return largestNumber;
    }