Skip to content

Commit 3d18514

Browse files
committed
Gigasecond solution
1 parent 34567e5 commit 3d18514

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

gigasecond/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Gigasecond
2+
3+
Calculate the moment when someone has lived for 10^9 seconds.
4+
5+
A gigasecond is 10^9 (1,000,000,000) seconds.
6+
7+
## Setup
8+
9+
Go through the setup instructions for JavaScript to
10+
install the necessary dependencies:
11+
12+
http://exercism.io/languages/javascript
13+
14+
## Making the Test Suite Pass
15+
16+
Execute the tests with:
17+
18+
jasmine-node .
19+
20+
In many test suites all but the first test have been skipped.
21+
22+
Once you get a test passing, you can unskip the next one by
23+
changing `xit` to `it`.
24+
25+
## Source
26+
27+
Chapter 9 in Chris Pine's online Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=09](http://pine.fm/LearnToProgram/?Chapter=09)
28+
29+
## Submitting Incomplete Problems
30+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
31+

gigasecond/gigasecond.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const Gigasecond = function (date) {
2+
this.value = date;
3+
};
4+
5+
const msToAdd = Math.pow(10, 9) * 1000;
6+
7+
Gigasecond.prototype.date = function () {
8+
const time = this.value.getTime();
9+
return new Date(time + msToAdd);
10+
};
11+
12+
module.exports = Gigasecond;

gigasecond/gigasecond.spec.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var Gigasecond = require('./gigasecond');
2+
3+
describe('Gigasecond', function() {
4+
5+
it('tells a gigasecond anniversary since midnight', function() {
6+
var gs = new Gigasecond(new Date(Date.UTC(2015, 8, 14)));
7+
var expectedDate = new Date(Date.UTC(2047, 4, 23, 1, 46, 40));
8+
expect(gs.date()).toEqual(expectedDate);
9+
});
10+
11+
it('tells the anniversary is next day when you are born at night', function() {
12+
var gs = new Gigasecond(new Date(Date.UTC(2015, 8, 14, 23, 59, 59)));
13+
var expectedDate = new Date(Date.UTC(2047, 4, 24, 1, 46, 39));
14+
expect(gs.date()).toEqual(expectedDate);
15+
});
16+
17+
it('even works before 1970 (beginning of Unix epoch)', function() {
18+
var gs = new Gigasecond(new Date(Date.UTC(1959, 6, 19, 5, 13, 45)));
19+
var expectedDate = new Date(Date.UTC(1991, 2, 27, 7, 0, 25));
20+
expect(gs.date()).toEqual(expectedDate);
21+
});
22+
23+
it('make sure calling "date" doesn\'t mutate value', function() {
24+
var gs = new Gigasecond(new Date(Date.UTC(1959, 6, 19, 5, 13, 45)));
25+
var expectedDate = new Date(Date.UTC(1991, 2, 27, 7, 0, 25));
26+
gs.date();
27+
expect(gs.date()).toEqual(expectedDate);
28+
});
29+
});
30+
31+

0 commit comments

Comments
 (0)