sumAll
This commit is contained in:
parent
11e496bcfd
commit
4b0fc07d5f
|
@ -1,55 +0,0 @@
|
||||||
# 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
|
|
||||||
const 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
|
|
||||||
const 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
|
|
||||||
const helloWorld = function() {
|
|
||||||
return 'Hello, World!'
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = helloWorld
|
|
||||||
```
|
|
||||||
|
|
||||||
For the most part we've set up these tests in such a way that you only have to write the code being tested. You should not have to worry about importing or exporting anything at this stage.. so just work around that bit of the code and write what it takes to make them pass!
|
|
||||||
|
|
||||||
Mohammed Nabeel's solutions:
|
|
||||||
https://github.com/mnabeelp/javascript-exercises/tree/master/helloWorld/
|
|
||||||
https://github.com/mnabeelp/javascript-exercises/tree/master/repeatString/
|
|
||||||
https://github.com/mnabeelp/javascript-exercises/tree/master/reverseString/
|
|
|
@ -1,5 +0,0 @@
|
||||||
const helloWorld = function() {
|
|
||||||
return 'Hello, World!'
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = helloWorld
|
|
|
@ -1,7 +0,0 @@
|
||||||
const helloWorld = require('./helloWorld');
|
|
||||||
|
|
||||||
describe('Hello World', function() {
|
|
||||||
it('says hello world', function() {
|
|
||||||
expect(helloWorld()).toEqual('Hello, World!');
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,14 +0,0 @@
|
||||||
This exercise is tricky and was removed from our recommendations because it mostly leverages regular expressions for the solution, and those aren't really taught at this point in our curriculum.
|
|
||||||
|
|
||||||
Leaving it here for posterity, or a good challenge for anyone that wants to give it a shot.
|
|
||||||
|
|
||||||
# Exercise XX - snakeCase
|
|
||||||
|
|
||||||
Convert phrases and words into snake case
|
|
||||||
|
|
||||||
> Snake case (or snake\_case) is the practice of writing compound words or phrases in which the elements are separated with one underscore character (\_) and no spaces, with each element's initial letter usually lowercased as in "foo\_bar"
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
snakeCase('Hello, World!') // hello_world
|
|
||||||
snakeCase('snakeCase') // snake_case
|
|
||||||
```
|
|
|
@ -1,5 +0,0 @@
|
||||||
const snakeCase = function() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = snakeCase
|
|
|
@ -1,23 +0,0 @@
|
||||||
const snakeCase = require('./snakeCase')
|
|
||||||
|
|
||||||
describe('snakeCase', function() {
|
|
||||||
it('works with simple lowercased phrases', function() {
|
|
||||||
expect(snakeCase('hello world')).toEqual('hello_world');
|
|
||||||
});
|
|
||||||
xit('works with Caps and punctuation', function() {
|
|
||||||
expect(snakeCase('Hello, World???')).toEqual('hello_world');
|
|
||||||
});
|
|
||||||
xit('works with longer phrases', function() {
|
|
||||||
expect(snakeCase('This is the song that never ends....')).toEqual('this_is_the_song_that_never_ends');
|
|
||||||
});
|
|
||||||
xit('works with camel case', function() {
|
|
||||||
expect(snakeCase('snakeCase')).toEqual('snake_case');
|
|
||||||
});
|
|
||||||
xit('works with kebab case', function() {
|
|
||||||
expect(snakeCase('snake-case')).toEqual('snake_case');
|
|
||||||
});
|
|
||||||
xit('works with WTF case', function() {
|
|
||||||
expect(snakeCase('SnAkE..CaSe..Is..AwEsOmE')).toEqual('snake_case_is_awesome');
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
|
@ -1,17 +0,0 @@
|
||||||
# Exercise 06 - tempConversion
|
|
||||||
|
|
||||||
Write two functions that convert temperatures from Fahrenheit to Celsius, and vice versa:
|
|
||||||
```
|
|
||||||
ftoc(32) // fahrenheit to celsius, should return 0
|
|
||||||
|
|
||||||
ctof(0) // celsius to fahrenheit, should return 32
|
|
||||||
```
|
|
||||||
|
|
||||||
Because we are human, we want the result temperature to be rounded to one decimal place: i.e., `ftoc(100)` should return `37.8` and not `37.77777777777778`.
|
|
||||||
|
|
||||||
This exercise asks you to create more than one function so the `module.exports` section of the spec file looks a little different this time. Nothing to worry about, we're just packaging both functions into a single object to be exported.
|
|
||||||
|
|
||||||
## Hints
|
|
||||||
- You can find the relevant formulae on [Wikipedia](https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature).
|
|
||||||
|
|
||||||
- Try to find by yourself on the Internet how to round a number to 1 decimal place in JavaScript. If you struggle, have a look [here](https://stackoverflow.com/q/7342957/5433628).
|
|
|
@ -1,12 +0,0 @@
|
||||||
const ftoc = function() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const ctof = function() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
ftoc,
|
|
||||||
ctof
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
const {ftoc, ctof} = require('./tempConversion')
|
|
||||||
|
|
||||||
describe('ftoc', function() {
|
|
||||||
it('works', function() {
|
|
||||||
expect(ftoc(32)).toEqual(0);
|
|
||||||
});
|
|
||||||
xit('rounds to 1 decimal', function() {
|
|
||||||
expect(ftoc(100)).toEqual(37.8);
|
|
||||||
});
|
|
||||||
xit('works with negatives', function() {
|
|
||||||
expect(ftoc(-100)).toEqual(-73.3);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('ctof', function() {
|
|
||||||
xit('works', function() {
|
|
||||||
expect(ctof(0)).toEqual(32);
|
|
||||||
});
|
|
||||||
xit('rounds to 1 decimal', function() {
|
|
||||||
expect(ctof(73.2)).toEqual(163.8);
|
|
||||||
});
|
|
||||||
xit('works with negatives', function() {
|
|
||||||
expect(ctof(-10)).toEqual(14);
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue