0
function largestInEach(arr) {
    var resultArray = [],
        highestValue = 0;
    for (var i = 0; i < arr.length; i++) {
        highestValue = arr[i].reduce(function(a, b){
            return a >= b ? a : b;
        });
        resultArray.push(highestValue);
    } 
    return resultArray;
}

Can someone please explain this code.

The rest of the code is very clear to me, but I have difficulty understanding the reduce function and its application.

4
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 17, 2015 at 9:23
  • Not sure if the italics will show up! If they don't, the part of the code I am having difficulty understanding is between the two asterisks. Commented Aug 17, 2015 at 9:25
  • Stack Overflow is for people looking for answers to problems they've attempted to solve but cannot find a solution. It is not for explaining code to teach people. I recommend looking up the documentation about the functions that you're not understanding. Commented Aug 17, 2015 at 9:28
  • Thats just great! Italics don't show up and neither do the two asterisks. Well, just for clarification (and I am not expecting SuperBiasedMan's help any time soon!) the part that I am having difficulties with is this part 'highestValue = arr[i].reduce(function(a, b){return a >= b ? a : b;' If someone can help me to understand this it would be highly appreciated, thanks. Commented Aug 17, 2015 at 9:54

3 Answers 3

1

I agree with most of the other comments that you should search more and do self learning. However, I know it is sometimes hard to find the exact info on how things work. So ill explain this to you.

Now coming to your code.

https://jsfiddle.net/Peripona/ppd4L9Lz/

It contains an array of arrays where you at the end create a resulted array with highest or lowest value elements from all the sub arrays.

like you got

var arry = [[1,2,3],[2,3,4],[20,-2,3]]

talking in layman terms...

you got one array if you sort or reduce an array of integers, it might not always generate what you say for example if you got this data

var ar = [1,3,34,11,0,13,7,17,-2,20,-21]

and if you do normal ar.sort() to get the sorted values

you would expect something like this... as output " [-21, -2, 0, 1, 3, 7, 11, 13, 17, 20, 34] "

but on the contrary you would get the output like this..

" [-2, -21, 0, 1, 11, 13, 17, 20, 3, 34, 7] "

Now you wonder... why this strange behavior and how does it matter anyhow to my Question..

It Does matter..

Cuz this is what you need to do to get the right output.. The way sort function is written has to work for for String and other types as well. so they convert data into other formats when doing comparison on sort.

So all in all if you pass a function inside and specify that you need the in ascending order that is a-b.. Descending order that is b-a.. ar.sort(function(a,b){return a-b;})

Now coming to another part that is Reduce this function takes a function argument and get you the highest or the lowest value from the array.

therefore if you do..

ar.reduce(function(a,b){return a>=b ? b : a})

will give you -21 as the output..

and if you do

ar.reduce(function(a,b){return a>=b ? a : b})

It will give you : 34

So this function will take multidimensional arrays where each array contains some digits and this function will get you the highest from all those arrays..

I hope this Explains everything.

Sign up to request clarification or add additional context in comments.

Comments

0

Reduce function allows you to go through each item in an array, where you will be able to see previous array value, and current array value, in your case:

a = previous value, b = current value,

-(not in there)-

i = index, currArray = the array you are working with.

and in your code you are comparing and returning if your previous value is greater than or equal to current value.

a >= b ? a : b;

Conditional (ternary) Operator which is (condition ? do this : or this ) -> Think of it like a if statement If(a >= b){ return a }else{ return b }

see Conditional (ternary) Operator

Also your 'arr' could be multi dimensional array. forexample Trying the following code on http://plnkr.co/edit/?p=preview

hit f12 for developer tools and look at console for results.

var arr = [[1,2],[4,3],[5,23,52]];

var resultArray = [],
var highestValue;
for (var i = 0; i < arr.length; i++) {
    highestValue = arr[i].reduce(function(a, b){
        return a >= b ? a : b;
    });
    resultArray.push(highestValue);
} 

console.log(resultArray);

You result array contains [2, 4, 52].

I hope this helps.

1 Comment

That does help. The part of the code that was eluding me was 'return a >= b ? : b;' If I now understand correctly: This part of the code basically extracts the highest value from arr and returns it, then later in the code that highest value is pushed to the resultArray, thank you for the explanation.
0

JS reduce method is applied against two values of array and reduce these two values of array ( a and b) into one (c) based on defined condition (return c = a+b ). Here in your case the condition was which among two is greater (a>b?a:b).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.