Skip to content

Commit

Permalink
move exercises to subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
kigiri committed Mar 15, 2018
1 parent 181285e commit e5a9702
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
max_line_length = 80
10 changes: 0 additions & 10 deletions capitalize.js

This file was deleted.

2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Commented code exemples for JavaScript
- [Methods](https://github.com/nan-academy/js-training/blob/master/examples/methods.js)
- [Ternary](https://github.com/nan-academy/js-training/blob/master/examples/ternary.js)
- [If / Else](https://github.com/nan-academy/js-training/blob/master/examples/if-else.js)
- [Loops](https://github.com/nan-academy/js-training/blob/master/examples/while.js)
- [Loops](https://github.com/nan-academy/js-training/blob/master/examples/loops.js)
- [Recursion](https://github.com/nan-academy/js-training/blob/master/examples/recursion.js)
- *[Functions as Values](https://github.com/nan-academy/js-training/blob/master/examples/functions-as-values.js)* **WIP**
- *[Callback](https://github.com/nan-academy/js-training/blob/master/examples/callback.js)* **WIP**
Expand Down
31 changes: 31 additions & 0 deletions examples/while.js → examples/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,34 @@ const times5 = n => {
// loop is over !
return result // return our result
}


/*
for..of
For..Of is a cleaner way to iterate over an iterable (Array, Strings, etc...)
It's the most usefull type of loops.
Here is the basic syntax :
for (const element of iterable) {
// code
}
*/

// Let's see how we could count how many letter O are in this sentence :
const sentence = 'How are you today ?'

let countOfTheLetterO = 0 // this will hold our total

// for each element of our string
// we assign it's value to a const variable named letter
for (const letter of sentence) { // our loop will stop on reaching the end
if (letter === 'o') {
// we have a match ! better add it to our total now...
countOfTheLetterO = countOfTheLetterO + 1
}
}

// and that's all there is to it !
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions exercises/capitalize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

/*
* Create a function `capitalize` takes a string
* and transform to upper case only the first letter
*
*/


//* Begin of tests
const assert = require('assert')

assert.strictEqual(typeof capitalize, "function")
assert.strictEqual(capitalize('str'), 'Str')
assert.strictEqual(capitalize('qsdqsdqsd'), 'Qsdqsdqsd')
assert.strictEqual(capitalize('STR'), 'Str')
assert.strictEqual(capitalize('zapZAP'), 'Zapzap')
// End of tests */
File renamed without changes.
File renamed without changes.
6 changes: 4 additions & 2 deletions cut.js → exercises/cut.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*
*/

// You must write your own tests
throw Error('No tests !')
//* Begin of tests
const assert = require('assert')

assert.fail('You must write your own tests')
// End of tests */
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions is-positive.js → exercises/is-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

// Your code:


//* Begin of tests
Expand Down
8 changes: 6 additions & 2 deletions jaden-case.js → exercises/jaden-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
*/


// You must write your own tests
throw Error('No tests !')

//* Begin of tests
const assert = require('assert')

assert.fail('You must write your own tests')
// End of tests */
8 changes: 6 additions & 2 deletions keep.js → exercises/keep.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@
*
*/

// You must write your own tests
throw Error('No tests !')

//* Begin of tests
const assert = require('assert')

assert.fail('You must write your own tests')
// End of tests */
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 6 additions & 2 deletions whisper.js → exercises/whisper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
*
*/

// You must write your own tests
throw Error('No tests !')

//* Begin of tests
const assert = require('assert')

assert.fail('You must write your own tests')
// End of tests */
File renamed without changes.
8 changes: 6 additions & 2 deletions yell.js → exercises/yell.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
*
*/

// You must write your own tests
throw Error('No tests !')

//* Begin of tests
const assert = require('assert')

assert.fail('You must write your own tests')
// End of tests */
Binary file modified img/where-to-code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 34 additions & 26 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
## Setup
First, fork this repository.
```sh
# download the code locally
git clone https://github.com/wildcodeschoolparis/js-training.git
# download your code locally
git clone https://github.com/$GITHUB_LOGIN/js-training.git

# change working directory to the newly cloned repository
cd js-training

# get on your own branch
git checkout -b mybranch
git checkout -b train
```

## Solving
You can start editing the javascript files with your favorite text editor.
```sh
# Sublime Text
subl primitive.js

# VSCode
code primitive.js

# Vim
vim primitive.js

# I can not be held responsable if you use anything else.
```

![where-to-code](https://github.com/wildcodeschoolparis/js-training/blob/master/img/where-to-code.png)

## Testing
Once you have coded your masterpiece, you can start to test your solutions.

Expand All @@ -35,37 +19,61 @@ You may either use our test tool to check all files
# run the test runner
./tester
```
![output-tester](https://github.com/wildcodeschoolparis/js-training/blob/master/img/output-tester.png)
![output-tester](https://github.com/nan-academy/js-training/blob/master/img/output-tester.png)

Or you can test files one by one with node
```sh
# open the file in node
node primitive.js
# open a file in node
node exercises/primitive
```
![output-node](https://github.com/wildcodeschoolparis/js-training/blob/master/img/output-node.png)
![output-node](https://github.com/nan-academy/js-training/blob/master/img/output-node.png)

*our tester may include bugs, feel free to check the code and fix it*

(in doubt use node)

## Solving
You can start editing the javascript files with your favorite text editor.
```sh
# Sublime Text
subl primitive.js

# VSCode
code primitive.js

# Vim
vim primitive.js

# I can not be held responsable if you use anything else.
```

![where-to-code](https://github.com/nan-academy/js-training/blob/master/img/where-to-code.png)

## Update
We may update tests, to apply them fetch and merge the master branch back to
your branch.

```sh

git merge pull push branch checkout origin master stash pop
```


## Test Style
We recommand to use [`editorconfig`](http://editorconfig.org/#download) to
auto-config your editor to match the coding style

We also made an `eslint` rule to help you formating your code.
We also made an `.eslint` rule to help you formating your code.

You may want to use [`eslint`](https://eslint.org/) and [`prettier-eslint`](https://github.com/prettier/prettier-eslint) for painless application of
the style


## Contribute
We greatly recommand you to add your own tests to this list

- add your test file *(ex: `exercises/my-test.js`)*
- edit `package.json` and add `"my-test"` to the `"tests"` array
- make a pull request !

All the tests must be written using [assert](https://nodejs.org/api/assert.html)
for the tester to work properly.
22 changes: 11 additions & 11 deletions tester
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const fs = require('fs')
const { performance: { now } } = require('perf_hooks')
const { promisify } = require('util')
const Module = require('module')
const { dirname } = require('path')
const { dirname, join } = require('path')
const assert = require('assert')
const pkg = require('./package.json')

Expand Down Expand Up @@ -45,7 +45,7 @@ const prettyStack = err => err.message.red()
+ ' ' + parseStackLine(err.stack.split('\n')
.filter(line => line.includes(current.filename))[0], err)

Object.keys(assert).forEach(key => {
Object.keys(assert).filter(key => key !== 'fail').forEach(key => {
const fn = assert[key]
const last = fn.length - 1
assert[key] = (...args) => {
Expand All @@ -60,29 +60,29 @@ Object.keys(assert).forEach(key => {
}
})

Promise.all(pkg.tests.map(async key => {
const filename = key + '.js'
const code = await readFile(`${__dirname}/${filename}`, 'utf-8')
return { code, filename }
})).then(codes => {
Promise.all(pkg.tests.map(async key => ({
code: await readFile(join(__dirname, 'exercises', key + '.js'), 'utf-8'),
filename: key + '.js',
key,
}))).then(codes => {
let failedTestCount = 0
for (const { code, filename } of codes) {
for (const { code, filename, key } of codes) {
current.length = 0
current.allPass = true
current.filename = filename

try {
(new Module(filename, module.parent))._compile(code, filename)
if (current.allPass) {
console.log('✓'.grn(), filename.slice(0, -3))
console.log('✓'.grn(), key)
} else {
failedTestCount++
console.log('-'.blu(), filename.slice(0, -3))
console.log('-'.blu(), key)
console.log(current.join('\n'))
}
} catch (err) {
if (!failedTestCount) {
console.log(`✗`.red(), filename.slice(0, -3), prettyStack(err))
console.log(`✗`.red(), key, prettyStack(err))
}
failedTestCount++
}
Expand Down

0 comments on commit e5a9702

Please sign in to comment.