Calculate the moment when someone has lived for 10^9 seconds.
A gigasecond is 10^9 (1,000,000,000) seconds.
Go through the setup instructions for JavaScript to install the necessary dependencies:
http://exercism.io/languages/javascript/installation
The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:
npm install -g jasmine
Run the test suite from the exercise directory with:
jasmine gigasecond.spec.js
In many test suites all but the first test have been marked "pending".
Once you get a test passing, activate the next one by changing xit
to it
.
Chapter 9 in Chris Pine's online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
var Gigasecond = require('./gigasecond');
describe('Gigasecond', function () {
it('tells a gigasecond anniversary since midnight', function () {
var gs = new Gigasecond(new Date(Date.UTC(2015, 8, 14)));
var expectedDate = new Date(Date.UTC(2047, 4, 23, 1, 46, 40));
expect(gs.date()).toEqual(expectedDate);
});
xit('tells the anniversary is next day when you are born at night', function () {
var gs = new Gigasecond(new Date(Date.UTC(2015, 8, 14, 23, 59, 59)));
var expectedDate = new Date(Date.UTC(2047, 4, 24, 1, 46, 39));
expect(gs.date()).toEqual(expectedDate);
});
xit('even works before 1970 (beginning of Unix epoch)', function () {
var gs = new Gigasecond(new Date(Date.UTC(1959, 6, 19, 5, 13, 45)));
var expectedDate = new Date(Date.UTC(1991, 2, 27, 7, 0, 25));
expect(gs.date()).toEqual(expectedDate);
});
xit('make sure calling "date" doesn\'t mutate value', function () {
var gs = new Gigasecond(new Date(Date.UTC(1959, 6, 19, 5, 13, 45)));
var expectedDate = new Date(Date.UTC(1991, 2, 27, 7, 0, 25));
gs.date();
expect(gs.date()).toEqual(expectedDate);
});
});
function Gigasecond(birthDay) {
this.birthDay = birthDay;
}
Gigasecond.prototype.date = function() {
var birthDaySeconds = this.birthDay.getTime() / 1000,
gigaDaySeconds = birthDaySeconds + Math.pow(10, 9);
return new Date(gigaDaySeconds * 1000);
};
module.exports = Gigasecond;
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments