|
| 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 | + |
0 commit comments