hello world

This commit is contained in:
Cody Loyd 2017-08-17 13:47:39 -05:00
parent b9a58bb8c7
commit 9bc987ea0f
4 changed files with 69 additions and 1 deletions

View file

@ -0,0 +1,5 @@
var helloWorld = function() {
return ''
}
module.exports = helloWorld

View file

@ -0,0 +1,47 @@
# Exercise 01 - Hello World.
The main purpose of this exercise is to walk you through the process of running the tests and make sure everything is set up and running correctly.
In this directory you will find 2 other files:
1. `helloWorld.js`
2. `helloWorld.spec.js`
This setup should be the same for all of the exercises. The plain javascript file is where you'll write your code, and the `spec` file contains the tests that verify your code is functional.
Let's look at the spec file first:
```javascript
var helloWorld = require('./helloWorld');
describe('Hello World', function() {
it('says hello world', function() {
expect(helloWorld()).toEqual('Hello, World!');
});
});
```
At the very top of the file we use `require()` to import the code from the javascript file (`helloWorld.js`) so that we can test it.
The next block (`describe()`) is the body of the test. Basically, all it's doing is running your code and testing to see if the output is correct. The `it()` function describes what should be happening in plain english and then includes the `expect()` function. For this simple example it should be pretty simple to read.
For now you do not need to worry about how to write tests, but you should try to get comfortable enough with the syntax to figure out what the tests are asking you to do. Go ahead and run the tests by entering `jasmine helloWorld.spec.js` in the terminal and watch it fail. The output from that command should tell you exactly what went wrong with your code. In this case, running the `helloWorld()` function should return the phrase 'Hello, World!' but instead it returns an empty string...
so let's look at the javascript file:
```javascript
var helloWorld = function() {
return ''
}
module.exports = helloWorld
```
In this file we have a simple function called helloWorld that returns an empty string... which is exactly what our test was complaining about. The `module.exports` on the last line is how we export the function so that it can be imported with `require()` in the spec file.
Go ahead and see if you can make the test pass by editing the return value of the function, and then running the test file again.
Just to make sure, in case you're confused at this point, the test is telling you that running the function helloWorld should return the phrase `Hello, World!`. Punctuation and capitalization definitely matter here, so double check that if the test still isn't passing.
this is what the final function should look like:
```javascript
var helloWorld = function() {
return 'Hello, World!'
}
module.exports = helloWorld
```

View file

@ -0,0 +1,7 @@
var helloWorld = require('./helloWorld');
describe('Hello World', function() {
it('says hello world', function() {
expect(helloWorld()).toEqual('Hello, World!');
});
});

View file

@ -1 +1,10 @@
These are a series of javascript exercises intended to be used alongside the curriculum at 'The Odin Project' They start very simply, but get more involved as you progress through them
## HOW TO USE THESE EXERCISES
Before you start you should have a few things installed on your machine:
1. NodeJS. To check if you have it type `node -v` in a terminal. If you get back a number that means you've got it installed. If not, you have a few options, [check here](https://nodejs.org/en/).
2. Jasmine. Jasmine is a testing framework for Javascript. Type `jasmine -v` to check for it. If you need to install it type `npm install -g jasmine` to do so.
Each exercise includes 3 files, a markdown file with a description of the task, an empty (or mostly empty) javascript file, and a set of tests. To complete the exercise go to the exercise directory in a terminal and run `jasmine filename.spec.js`. This should find and run the test file and show you the output. Upon first running the tests you will find that the tests fail: this is by design! Your task is to open up the javascript file and write the code needed to get all of the tests to pass.
The first exercise, `helloWorld` will walk you through the process in more depth.