Skip to content

Commit b77b102

Browse files
committed
Merge pull request coffeescript-cookbook#7 from dmoulton/master
Max Array Value Recipe
2 parents 8e9842e + 21ef4ae commit b77b102

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
layout: recipe
3+
title: Max Array Value
4+
chapter: Arrays
5+
---
6+
7+
h2. Problem
8+
9+
You need to find the largest value contained in an array
10+
11+
h2. Solution
12+
13+
Use javascript's Math.max, along with a for loop
14+
15+
{% highlight coffeescript %}
16+
max = Math.max(max or= num, num) for num in [12,32,11,67,1,3]
17+
# => 67
18+
{% endhighlight %}
19+
20+
h2. Discussion
21+
22+
or= takes care of the initial assignment

0 commit comments

Comments
 (0)