Last active
December 17, 2015 15:29
-
-
Save haiiro-shimeji/5632652 to your computer and use it in GitHub Desktop.
range() #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| TestCase("range", { | |
| "test of range": function() { | |
| function range() { | |
| var s, e, i, j; | |
| if (2 < arguments.length) { | |
| s = arguments[0]; | |
| e = arguments[1]; | |
| i = arguments[2]; | |
| if (0 === i) { | |
| throw new Error("step must be not zero."); | |
| } | |
| } else if (1 < arguments.length) { | |
| s = arguments[0]; | |
| e = arguments[1]; | |
| i = 1; | |
| } else if (0 < arguments.length) { | |
| s = 0; | |
| e = arguments[0]; | |
| i = 1; | |
| } else { | |
| throw new Error("more than 1 arguments is required."); | |
| } | |
| var result = []; | |
| if (0 < i) { | |
| for (j=s; e>j; j+=i) { | |
| result.push(j); | |
| } | |
| } else { | |
| for (j=s; e<j; j+=i) { | |
| result.push(j); | |
| } | |
| } | |
| return result; | |
| } | |
| assertEquals([1, 2, 3, 4], range(1, 5)); | |
| assertEquals([0, 1, 2, 3], range(0, 4)); | |
| assertEquals([-1, 0], range(-1, 1)); | |
| assertEquals([], range(5, 2)); | |
| assertEquals([5, 4, 3], range(5, 2, -1)); | |
| assertEquals([], range(2, 5, -1)); | |
| assertEquals([0, 1, 2, 3, 4], range(5)); | |
| assertException(range.bind(null, 1, 5, 0), "Error"); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment