CoffeeScript Ranges - CoffeeScript

What is CoffeeScript Ranges?

In the preceding chapter, we have seen Arrays in CoffeeScript; at the same time as programming we will face a few scenarios where we should keep a sequence of numerical values in an array as proven underneath.

CoffeeScript gives a shorter manner of expressing the arrays containing a sequence of numerical values, known as degrees. This feature of CoffeeScript is inspired from Ruby.

Syntax

Ranges are created by two numerical values, the first and remaining positions within the variety, separated via... Or ... With dots (1...4), the range is inclusive (1, 2, 3, 4); with 3 dots (1...4), the range excludes the end (1, 2, 3).

Given under is the syntax of levels in CoffeeScript. We will define the values in a range between square braces [ ] just like arrays. In stages, at the same time as storing a chain of numerical values, in place of presenting the values of the entire series, we can simply specify its begin and stop values separated with the aid of two dots (..) as shown beneath.

Example

Here is an example of ranges in CoffeeScript. Save this in a file with name ranges_example.coffee.

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript. Here you can observe that the range is converted in to complete CoffeeScript array.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output.

Excluding the end value

The stages are compiled into complete arrays containing all numbers. If we want to exclude the end price, then we must separate the start and end factors of the variety the use of three dots (...) as shown under.

Example

We can rewrite the above example by excluding the end value as shown below. Save the following contents in a file with name range_excluding_end.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.

Using Ranges with Variables

We can also define a range by assigning the start and end values to variables.

Example

Consider the following example. Here we have defined a range using variables. Save this code in a file with name range_variables.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.

Ranges with Arrays

We can slice arrays by using them with ranges. Whenever we specify ranges immediately after arrays (variables), then the CoffeeScript compiler converts it in to a slice() method call of JavaScript

.Assume that we have an array having numerical values, say 0 to 9, then we can retrieve the first 4 elements of it as shown below.

Negative values represent the elements from the end, for example, -1 indicates 9. If we specify a negative number 3 followed by two dots, the last three elements of the array will be extracted.

If we specify only two dots in the range of an array as num[..], then the complete array will be extracted. We can also replace an array segment with other elements using ranges as shown below.

Example

The following example demonstrates the use of ranges with arrays. Save this code in a file with name range_arrays.coffee
#slicing an array using ranges

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript. Here you can observe that all the ranges are converted in to the slice() method calls of JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.


On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.

Ranges with Strings

We can also use ranges with Strings. If we specify ranges after Strings, then CoffeeScript slices them and returns a new subset of characters.

Example

The following example demonstrates the use of ranges with Strings. Here we have created a string and extracted a substring from it using ranges. Save this code in a file with name ranges_with_strings.coffee

Open the command prompt and compile the .coffee file as shown below.

On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.

Comprehensions over Ranges

As objects and arrays, we can also iterate the elements of a range using comprehensions.

Example

Following is an example of using comprehensions over ranges. Here we have created a range and retrieved the elements in it using comprehensions. Save this code in a file with the name comprehensions_over_ranges.coffee

Open the command prompt and compile the .coffee file as shown below.



On compiling, it gives you the following JavaScript.

Now, open the command prompt again and run the CoffeeScript file as shown below.

On executing, the CoffeeScript file produces the following output. In here, you can observe that the end value 9 is excluded.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics