-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
432a2e4
commit 71bf639
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# fromTo | ||
|
||
Interview question of the [issue #339 of rendezvous with cassidoo](https://buttondown.email/cassidoo/archive/youve-got-to-love-whats-yours-alicia-keys/). | ||
|
||
## The Question | ||
|
||
**Write a function that produces a generator that produces values in a range.** | ||
|
||
Example: | ||
|
||
```js | ||
let range = fromTo(0,3) | ||
|
||
> range() | ||
0 | ||
> range() | ||
1 | ||
> range() | ||
2 | ||
> range() | ||
null | ||
``` | ||
|
||
## Installing & Running | ||
|
||
Just `pnpm i` to install all dependencies and then `pnpm t` to run the tests! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { getDirection, fromTo } from './fromTo'; | ||
|
||
describe('#getDirection', () => { | ||
it('should positive direction', () => { | ||
expect(getDirection(0, 3)).toBe(1); | ||
expect(getDirection(0, 0)).toBe(1); | ||
}); | ||
|
||
it('should negative direction', () => { | ||
expect(getDirection(3, 0)).toBe(-1); | ||
}); | ||
}); | ||
|
||
describe('#fromTo', () => { | ||
it('should generate values in the specified range', () => { | ||
const range = fromTo(0, 3); | ||
expect(range()).toBe(0); | ||
expect(range()).toBe(1); | ||
expect(range()).toBe(2); | ||
expect(range()).toBeNull(); | ||
}); | ||
|
||
it('should generate values in single-step range', () => { | ||
const range = fromTo(0, 0); | ||
expect(range()).toBeNull(); | ||
}); | ||
|
||
it('should generate values in an inverse range', () => { | ||
const range = fromTo(3, 0); | ||
expect(range()).toBe(3); | ||
expect(range()).toBe(2); | ||
expect(range()).toBe(1); | ||
expect(range()).toBeNull(); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Write a function that produces a generator that produces values in a range. | ||
Example: | ||
let range = fromTo(0,3) | ||
> range() | ||
0 | ||
> range() | ||
1 | ||
> range() | ||
2 | ||
> range() | ||
null | ||
*/ | ||
|
||
export function getDirection(from: number, to: number) { | ||
return (-1) ** Number(from > to); | ||
} | ||
|
||
export function fromTo(from: number, to: number): () => number | null { | ||
const direction = getDirection(from, to); | ||
from -= direction; | ||
|
||
return () => { | ||
from += direction; | ||
return from === to ? null : from; | ||
}; | ||
} |