Skip to content

Commit b436018

Browse files
committed
Max Array Value: Add splats example. See coffeescript-cookbook#39.
1 parent 9203cb0 commit b436018

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

chapters/arrays/max-array-value.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ You need to find the largest value contained in an array.
99

1010
## Solution
1111

12-
In ECMAScript 5, you can use `Array#reduce`. For compatibility with older javascripts, use Math.max.apply:
12+
You can use Math.max() JavaScript method along with splats.
13+
14+
{% highlight coffeescript %}
15+
Math.max [12, 32, 11, 67, 1, 3]...
16+
# => 67
17+
{% endhighlight %}
18+
19+
Alternatively, it's possible to use ES5 `reduce` method. For backward compatibility with older JavaScript implementations, use Math.max.apply:
1320

1421
{% highlight coffeescript %}
1522
# ECMAScript 5
@@ -23,4 +30,4 @@ Math.max.apply(null, [12,32,11,67,1,3])
2330

2431
## Discussion
2532

26-
`Math.max` compares two numbers and returns the larger of the two; the rest of this recipe (both versions) is just iterating over the array to find the largest one.
33+
`Math.max` compares every argument and returns the largest number from arguments. The ellipsis (`...`) converts every array value into argument which is given to the function. You can also use it with other functions which take variable ammount of arguments, such as `console.log`.

0 commit comments

Comments
 (0)