Skip to content

Commit 75cfaa4

Browse files
committed
Initial import
0 parents  commit 75cfaa4

File tree

8 files changed

+351
-0
lines changed

8 files changed

+351
-0
lines changed

README

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# exercism-javascript
2+
3+
[Exercism](http://exercism.io)'s **Javascript** track

hello-world/README.md

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Hello World
2+
3+
The classical introductory exercise. Just say "Hello, World!"
4+
5+
["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is
6+
the traditional first program for beginning programming in a new language
7+
or environment.
8+
9+
The objectives are simple:
10+
11+
- Write a function that returns the string "Hello, World!".
12+
- Run the test suite and make sure that it succeeds.
13+
- Submit your solution and check it at the website.
14+
15+
If everything goes well, you will be ready to fetch your first real exercise.
16+
17+
## Tutorial
18+
19+
This exercise has two files:
20+
21+
- hello-world.js
22+
- hello-world.spec.js
23+
24+
The first file is where you will write your code.
25+
The second is where the tests are defined.
26+
27+
The tests will check whether your code is doing the right thing.
28+
You don't need to be able to write a test suite from scratch,
29+
but it helps to understand what a test looks like, and what
30+
it is doing.
31+
32+
Open up the test file, hello-world.spec.js.
33+
It has three tests defined in it.
34+
35+
This is the first test:
36+
37+
it('says hello world with no name', function() {
38+
expect(helloWorld.hello('')).toEqual('Hello, World!');
39+
});
40+
41+
Run the test now, with the following command on the command-line:
42+
43+
jasmine-node .
44+
45+
The test fails, which makes sense since you've not written any code yet.
46+
47+
The failure looks like this:
48+
49+
1) Hello World says hello world with no name
50+
Message:
51+
Expected undefined to equal 'Hello, World!'.
52+
53+
There's more, but this is the most important part.
54+
55+
Take a look at that first line:
56+
57+
1) Hello World says hello world with no name
58+
59+
Now look at the test definition again:
60+
61+
it('says hello world with no name', function() {
62+
// ... more code here ...
63+
});
64+
65+
The text 'says hello world with no name' is repeated.
66+
This is how you know which test failed.
67+
68+
The failure message explains what is wrong:
69+
70+
Expected undefined to equal 'Hello, World!'.
71+
72+
This comes from the part of the test definition that says "expect":
73+
74+
expect(helloWorld.hello('')).toEqual('Hello, World!');
75+
76+
It's comparing two values. It is calling
77+
78+
helloWorld.hello('')
79+
80+
and comparing the result to a hard-coded string.
81+
82+
'Hello, World!'.
83+
84+
So if you look at the failure message again, the hello function
85+
is returning undefined.
86+
87+
Try changing the function in hello-world.js so that it says
88+
89+
HelloWorld.prototype.hello = function(input) {
90+
return "chocolate";
91+
};
92+
93+
Then run the tests again from the command-line:
94+
95+
jasmine-node .
96+
97+
Notice how it changes the failure message.
98+
99+
Then change the implementation in hello-world.js again, this time to make the test pass.
100+
101+
Once the test is passing, look at the second test in hello-world.spec.js. It looks like this:
102+
103+
xit('says hello to bob', function() {
104+
expect(helloWorld.hello('Bob')).toEqual('Hello, Bob!');
105+
});
106+
107+
This test starts with `xit` instead of `it`.
108+
That means that when jasmine-node runs the tests,
109+
the test will be skipped.
110+
111+
Change the test so that it starts with `it`,
112+
and run the tests again.
113+
114+
Make the test pass, and then do the same with the third test.
115+
116+
When you are done, submit your solution to exercism:
117+
118+
exercism submit hello-world.js
119+
120+
## Setup
121+
122+
Go through the setup instructions for JavaScript to
123+
install the necessary dependencies:
124+
125+
http://exercism.io/languages/javascript
126+
127+
## Making the Test Suite Pass
128+
129+
Execute the tests with:
130+
131+
jasmine-node .
132+
133+
In many test suites all but the first test have been skipped.
134+
135+
Once you get a test passing, you can unskip the next one by
136+
changing `xit` to `it`.
137+
138+
## Source
139+
140+
This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program)
141+
142+
## Submitting Incomplete Problems
143+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
144+

hello-world/hello-world.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// This is a stub file for the 'Hello World' exercise. It's been provided as a
3+
// convenience to get you started writing code faster.
4+
// Make sure to look at hello-world.spec.js--that should give you some hints about what is
5+
// expected here.
6+
7+
var HelloWorld = function() {};
8+
9+
HelloWorld.prototype.hello = function(input) {
10+
const name = input ? input : 'World';
11+
return `Hello, ${name}!`;
12+
};
13+
14+
module.exports = HelloWorld;

hello-world/hello-world.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var HelloWorld = require('./hello-world');
2+
3+
describe('Hello World', function() {
4+
var helloWorld = new HelloWorld();
5+
6+
it('says hello world with no name', function() {
7+
expect(helloWorld.hello('')).toEqual('Hello, World!');
8+
});
9+
10+
it('says hello to bob', function() {
11+
expect(helloWorld.hello('Bob')).toEqual('Hello, Bob!');
12+
});
13+
14+
it('says hello to sally', function() {
15+
expect(helloWorld.hello('Sally')).toEqual('Hello, Sally!');
16+
});
17+
});

leap/HINT.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
This is the first test for this exercise:
2+
3+
```javascript
4+
it('is not very common', function() {
5+
var year = new Year(2015);
6+
expect(year.isLeap()).toBe(false);
7+
});
8+
```
9+
10+
Each test in the exercise follows the same pattern:
11+
12+
1. A new Year is instantiated with a value and stored in a variable: year.
13+
2. The test calls an instance method, isLeap(), from the variable `year`.
14+
15+
The `Year` function is a constructor, which means that it is specifically designed to provide a template for new objects.
16+
17+
When a new `Year` object is created:
18+
19+
```javascript
20+
var year = new Year(2015);
21+
```
22+
23+
We expect `year` to, at the very least, have a specific value (in this case 2015) assigned to it. Otherwise it would not represent an actual year.
24+
25+
This means that we must store the value passed as a parameter when the new `Year` is created (2015), so that the new `Year` can access it.
26+
27+
The way a constructor stores these fundamental values (instance variables) is like this:
28+
29+
```javascript
30+
var Constructor = function(input) {
31+
this.value = input;
32+
};
33+
```
34+
35+
The `this` in the constructor refers to the newly created instance of the `Constructor`.
36+
37+
Once the input (parameter) is stored in an instance variable, any instance methods, such as:
38+
39+
```javascript
40+
Constructor.prototype.instanceMethod = function() {
41+
// this method can now access the input by calling `this.value`
42+
};
43+
```
44+
45+
The instance method accesses the input using `this.value` (in this example).
46+
47+
This is why code needs to be written in two separate functions:
48+
49+
1. The first function, `Year`, is a constructor that serves as a template for year objects that are created with their value (such as 2015).
50+
This function needs to store the input when a new `Year` is created.
51+
2. The second function, isLeap(), is an instance method that is called from a new `year`.
52+
This function (method) should contain the logic to determine if the given year is a leap year.

leap/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Leap
2+
3+
Given a year, report if it is a leap year.
4+
5+
The tricky thing here is that a leap year in the Gregorian calendar occurs:
6+
7+
```plain
8+
on every year that is evenly divisible by 4
9+
except every year that is evenly divisible by 100
10+
unless the year is also evenly divisible by 400
11+
```
12+
13+
For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap
14+
year, but 2000 is.
15+
16+
If your language provides a method in the standard library that does
17+
this look-up, pretend it doesn't exist and implement it yourself.
18+
19+
## Notes
20+
21+
Though our exercise adopts some very simple rules, there is more to
22+
learn!
23+
24+
For a delightful, four minute explanation of the whole leap year
25+
phenomenon, go watch [this youtube video][video].
26+
27+
[video]: http://www.youtube.com/watch?v=xX96xng7sAE
28+
29+
## Setup
30+
31+
Go through the setup instructions for JavaScript to
32+
install the necessary dependencies:
33+
34+
http://exercism.io/languages/javascript
35+
36+
## Making the Test Suite Pass
37+
38+
Execute the tests with:
39+
40+
jasmine-node .
41+
42+
In many test suites all but the first test have been skipped.
43+
44+
Once you get a test passing, you can unskip the next one by
45+
changing `xit` to `it`.
46+
47+
## Source
48+
49+
JavaRanch Cattle Drive, exercise 3 [http://www.javaranch.com/leap.jsp](http://www.javaranch.com/leap.jsp)
50+
51+
## Submitting Incomplete Problems
52+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
53+

leap/leap.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// This is only a SKELETON file for the "Leap" exercise. It's been provided as a
3+
// convenience to get you started writing code faster.
4+
//
5+
6+
var Year = function(input) {
7+
this.year = input;
8+
};
9+
10+
Year.prototype.isDivisibleBy = function (divider) {
11+
return this.year % divider === 0;
12+
};
13+
14+
Year.prototype.isLeap = function() {
15+
return this.isDivisibleBy(4) && !this.isDivisibleBy(100) || this.isDivisibleBy(400);
16+
};
17+
18+
module.exports = Year;

leap/leap.spec.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
var Year = require('./leap');
2+
3+
describe('Leap year', function() {
4+
5+
it('is not very common', function() {
6+
var year = new Year(2015);
7+
expect(year.isLeap()).toBe(false);
8+
});
9+
10+
it('is introduced every 4 years to adjust about a day', function() {
11+
var year = new Year(2016);
12+
expect(year.isLeap()).toBe(true);
13+
});
14+
15+
it('is skipped every 100 years to remove an extra day', function() {
16+
var year = new Year(1900);
17+
expect(year.isLeap()).toBe(false);
18+
});
19+
20+
it('is reintroduced every 400 years to adjust another day', function() {
21+
var year = new Year(2000);
22+
expect(year.isLeap()).toBe(true);
23+
});
24+
25+
// Feel free to enable the following tests to check some more examples
26+
describe('Additional example of a leap year that', function () {
27+
28+
it('is not a leap year', function () {
29+
var year = new Year(1978);
30+
expect(year.isLeap()).toBe(false);
31+
});
32+
33+
it('is a common leap year', function () {
34+
var year = new Year(1992);
35+
expect(year.isLeap()).toBe(true);
36+
});
37+
38+
it('is skipped every 100 years', function () {
39+
var year = new Year(2100);
40+
expect(year.isLeap()).toBe(false);
41+
});
42+
43+
it('is reintroduced every 400 years', function () {
44+
var year = new Year(2400);
45+
expect(year.isLeap()).toBe(true);
46+
});
47+
48+
});
49+
50+
});

0 commit comments

Comments
 (0)