Skip to content

Commit d8cf99b

Browse files
committed
initial implementation of _.range
1 parent 67f1e8a commit d8cf99b

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

test/collections.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ $(document).ready(function() {
22

33
module("Collection functions (each, any, select, and so on...)");
44

5+
test("generators: range", function() {
6+
equals(_.range(4).join(' '), '0 1 2 3 4', 'range with positive number generates an array of of elements 0,1,2,...,n');
7+
});
8+
59
test("collections: each", function() {
610
_.each([1, 2, 3], function(num, i) {
711
equals(num, i + 1, 'each iterators provide value and iteration count');

underscore.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@
3333
// Current version.
3434
_.VERSION = '0.4.5';
3535

36+
/*------------------------ Generator Functions: ----------------------------*/
37+
_.range = function(upper, lower, step) {
38+
if (!lower) var lower = 0;
39+
if (!step) var step = 1;
40+
41+
var result = new Array(((upper - lower) / step) + 1));
42+
43+
for (var i = lower; i <= upper; i += step) {
44+
result[i] = i;
45+
}
46+
47+
return result;
48+
}
49+
3650
/*------------------------ Collection Functions: ---------------------------*/
3751

3852
// The cornerstone, an each implementation.

0 commit comments

Comments
 (0)