how tow write a function max withe reduce | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

how tow write a function max withe reduce

4th Apr 2020, 3:13 PM
Yasser Massoud
Yasser Massoud - avatar
2 Answers
0
Reduce applies a function that takes two values and returns one. This is applied iteratively to all pairs of array[i], arrai[i+1], until only one value is left. If you want to add up all the numbers, you'd write a function that adds two numbers. So what would you need to return from the function, if you wanted the biggest value in the end? What would you have to do with each pair?
4th Apr 2020, 3:31 PM
HonFu
HonFu - avatar
0
var max = arr => arr.reduce((a,v) => a<v ? v : a);
4th Apr 2020, 5:43 PM
visph
visph - avatar