mirror of https://github.com/01-edu/public.git
Clement Denis
5 years ago
2 changed files with 136 additions and 0 deletions
@ -0,0 +1,64 @@
|
||||
## Block Chain |
||||
|
||||
### Instructions |
||||
|
||||
Create a `blockChain` that create a block in your very own block chain. |
||||
|
||||
the function takes 2 arguments: |
||||
- `data` any valid JSON data |
||||
- `prev` the previous block, if no block are given it should use the |
||||
genesis block: `{ index: 0, hash: '0' }` |
||||
|
||||
A block must have the following properties: |
||||
- `index` |
||||
- `hash` a computed hash using the concatenation of the `index`, `hash` |
||||
and stringified `data` and hashing all of it using the provided `hashCode`. |
||||
- `data` the data (not encoded in JSON) |
||||
- `prev` the previous block |
||||
- `chain` a function that takes a new `data` and create the next block with it. |
||||
|
||||
|
||||
### Examples |
||||
|
||||
```js |
||||
const first = blockChain({ a: 1 }) |
||||
console.log(first.index) // -> 1 |
||||
console.log(first.data) // -> { a: 1 } |
||||
console.log(first.prev) // -> { index: 0, hash: "0" } |
||||
console.log(first.hash) // -> '1103f27' |
||||
console.log(hashCode('10{"a":1}')) // -> '1103f27' |
||||
|
||||
const second = first.chain({ hello: 'world' }) |
||||
console.log(second.hash) // -> '18drvvc' |
||||
console.log(hashCode('21103f27{"hello":"world"}')) // -> '18drvvc' |
||||
|
||||
const chain = second |
||||
.chain({ value: 4455 }) |
||||
.chain({ some: 'data' }) |
||||
.chain({ cool: 'stuff' }) |
||||
|
||||
const fork = second |
||||
.chain({ value: 335 }) |
||||
.chain({ some: 'data' }) |
||||
.chain({ cool: 'stuff' }) |
||||
|
||||
console.log(chain.hash) // -> '1qr3qfs' |
||||
console.log(fork.hash) // -> '1x9gsc1' |
||||
console.log(chain.index) // -> 5 |
||||
console.log(fork.index) // -> 5 |
||||
``` |
||||
|
||||
|
||||
### Notions |
||||
|
||||
- [devdocs.io/javascript/global_objects/json/stringify](https://devdocs.io/javascript/global_objects/json/stringify) |
||||
|
||||
|
||||
### Code provided |
||||
|
||||
```js |
||||
const hashCode = str => |
||||
( |
||||
[...str].reduce((h, c) => (h = (h << 5) - h + c.charCodeAt(0)) & h, 0) >>> 0 |
||||
).toString(36) |
||||
``` |
@ -0,0 +1,72 @@
|
||||
## Collections |
||||
|
||||
### Instructions |
||||
|
||||
Write a bunch of function to move from one type to another |
||||
|
||||
- `arrToSet` from `Array` to `Set` |
||||
- `arrToStr` from `Array` to `String` |
||||
- `setToArr` from `Set` to `Array` |
||||
- `setToStr` from `Set` to `String` |
||||
- `strToArr` from `String` to `Array` |
||||
- `strToSet` from `String` to `Set` |
||||
- `mapToObj` from `Map` to `Object` |
||||
- `objToArr` from `Object` to `Array` |
||||
- `objToMap` from `Object` to `Map` |
||||
- `arrToObj` from `Array` to `Object` |
||||
- `strToObj` from `String` to `Object` |
||||
|
||||
and finally write a function `superTypeOf` that unlike `typeof` return |
||||
a different value for advanced types like `Map` and `Set` |
||||
|
||||
|
||||
### Examples |
||||
|
||||
```js |
||||
const str = 'hello' |
||||
const arr = [ 1, 2, 1, 3 ] |
||||
const obj = { x: 45, y: 75, radius: 24 } |
||||
const set = new Set() |
||||
const map = new Map() |
||||
set.add(1) |
||||
set.add(2) |
||||
set.add(1) |
||||
set.add(3) |
||||
map.add('a', 1) |
||||
map.add('b', 2) |
||||
map.add(3, 'c') |
||||
map.add(4, 'd') |
||||
|
||||
arrToSet(arr) // -> Set { 1, 2, 3 } |
||||
arrToStr(arr) // -> '1213' |
||||
setToArr(set) // -> [1, 2, 3] |
||||
setToStr(set) // -> '123' |
||||
strToArr(str) // -> ['h', 'e', 'l', 'l', 'o'] |
||||
strToSet(str) // -> Set { 'h', 'e', 'l', 'o' } |
||||
mapToObj(map) // -> { a: 1, b: 2, '3': 'c', '4': 'd' } |
||||
objToArr(obj) // -> [45, 75, 24] |
||||
objToMap(obj) // -> Map { 'x' => 45, 'y' => 75, 'radius' => 24 } |
||||
arrToObj(arr) // -> { '0': 1, '1': 2, '3': 1, '4': 3 } |
||||
strToObj(str) // -> { '0': 'h', '1': 'e', '2': 'l', '3': 'l', '4': 'o' } |
||||
|
||||
superTypeOf(map) // -> 'Map' |
||||
superTypeOf(set) // -> 'Set' |
||||
superTypeOf(obj) // -> 'Object' |
||||
superTypeOf(str) // -> 'String' |
||||
superTypeOf(666) // -> 'Number' |
||||
superTypeOf(NaN) // -> 'Number' |
||||
superTypeOf(arr) // -> 'Array' |
||||
superTypeOf(null) // -> 'null' |
||||
superTypeOf(undefined) // -> 'undefined' |
||||
superTypeOf(superTypeOf) // -> 'Function' |
||||
``` |
||||
|
||||
|
||||
### Notions |
||||
|
||||
- [devdocs.io/javascript/operators/typeof](https://devdocs.io/javascript/operators/typeof) |
||||
- [devdocs.io/javascript/operators/spread_syntax](https://devdocs.io/javascript/operators/spread_syntax) |
||||
- [devdocs.io/javascript/global_objects/map](https://devdocs.io/javascript/global_objects/map) |
||||
- [devdocs.io/javascript/global_objects/set](https://devdocs.io/javascript/global_objects/set) |
||||
- [devdocs.io/javascript/global_objects/object/fromentries](https://devdocs.io/javascript/global_objects/object/fromentries) |
||||
- [devdocs.io/javascript/global_objects/object/entries](https://devdocs.io/javascript/global_objects/object/entries) |
Loading…
Reference in new issue