Skip to content

Commit 3d624a3

Browse files
committed
Added a new recipe for define range in an array using CoffeeScript
1 parent 2e8601c commit 3d624a3

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

chapters/arrays/define-ranges.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
layout: recipe
3+
title: Define Ranges Array
4+
chapter: Arrays
5+
---
6+
## Problem
7+
8+
You want to define range in array.
9+
10+
## Solution
11+
12+
There are two method to define range of an array element in CoffeeScript.
13+
14+
{% highlight coffeescript %}
15+
16+
myArray = [1..10]
17+
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
18+
19+
{% endhighlight %}
20+
21+
{% highlight coffeescript %}
22+
23+
myArray = [1...10]
24+
# => [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
25+
26+
{% endhighlight %}
27+
28+
We can also reverse the range of element by writing it this way.
29+
30+
{% highlight coffeescript %}
31+
32+
myLargeArray = [10..1]
33+
# => [ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
34+
35+
{% endhighlight %}
36+
37+
{% highlight coffeescript %}
38+
myLargeArray = [10...1]
39+
# => [ 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
40+
41+
{% endhighlight %}
42+
43+
## Discussion
44+
45+
Inclusive range always define by '..' operator.
46+
47+
Exclusive range difine by '...', and always omit the last value.

0 commit comments

Comments
 (0)