The classical introductory exercise. Just say "Hello, World!".
"Hello, World!" is the traditional first program for beginning programming in a new language or environment.
The objectives are simple:
If everything goes well, you will be ready to fetch your first real exercise.
This exercise has two files:
The first file is where you will write your code. The second is where the tests are defined.
The tests will check whether your code is doing the right thing. You don't need to be able to write a test suite from scratch, but it helps to understand what a test looks like, and what it is doing.
Open up the test file, hello-world.spec.js. There is one test inside:
it('says hello world', function() {
expect(helloWorld.hello()).toEqual('Hello, World!');
});
Run the test now, with the following command on the command-line:
jasmine hello-world.spec.js
The test fails, which makes sense since you've not written any code yet.
The failure looks like this:
1) Hello World says hello world
Message:
Expected undefined to equal 'Hello, World!'.
There's more, but this is the most important part.
Take a look at that first line:
1) Hello World says hello world
Now look at the test definition again:
it('says hello world', function() {
// ... more code here ...
});
The text 'says hello world' is repeated. This is how you know the test failed.
The failure message explains what is wrong:
Expected undefined to equal 'Hello, World!'.
This comes from the part of the test definition that says "expect":
expect(helloWorld.hello()).toEqual('Hello, World!');
It's comparing two values. It is calling
helloWorld.hello()
and comparing the result to a hard-coded string.
'Hello, World!'.
So if you look at the failure message again, the hello function is returning undefined.
Try changing the function in hello-world.js so that it says
HelloWorld.prototype.hello = function(input) {
return "chocolate";
};
Then run the tests again from the command-line:
jasmine hello-world.spec.js
Notice how it changes the failure message.
Then change the implementation in hello-world.js again, this time to make the test pass.
When you are done, submit your solution to exercism:
exercism submit hello-world.js
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 hello-world.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
.
This is an exercise to introduce users to using Exercism http://en.wikipedia.org/wiki/%22Hello,_world!%22_program
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
var HelloWorld = require('./hello-world');
describe('Hello World', function () {
var helloWorld = new HelloWorld();
it('says hello world', function () {
expect(helloWorld.hello()).toEqual('Hello, World!');
});
});
var HelloWorld = function() {};
HelloWorld.prototype.is_addressable = function(str) {
if (typeof str === 'undefined' || str === null) {
return false;
}
if (str.length > 0 && typeof str.valueOf() === 'string') {
return true;
}
if (typeof str === 'number') {
return true;
}
return false;
};
HelloWorld.prototype.hello = function(input) {
var name = 'world';
if (this.is_addressable(input)) {
name = input;
}
return 'Hello, ' + name + '!';
};
module.exports = HelloWorld;
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,450 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