From 2056262a006f801f9b0dd9b61fc2c22f38c9473f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A4rt=20Kalmo?= Date: Thu, 30 Mar 2017 13:59:37 +0300 Subject: [PATCH] done state --- .gitignore | 5 +++++ data/db.json | 23 +++++++++++++++++++++++ data/routes.json | 3 +++ ex1.js | 19 +++++++++++++++++++ ex2.js | 12 ++++++++++++ ex3.js | 19 +++++++++++++++++++ ex4.js | 20 ++++++++++++++++++++ ex5.js | 22 ++++++++++++++++++++++ ex6.js | 34 ++++++++++++++++++++++++++++++++++ package.json | 11 +++++++++++ 10 files changed, 168 insertions(+) create mode 100644 .gitignore create mode 100644 data/db.json create mode 100644 data/routes.json create mode 100644 ex1.js create mode 100644 ex2.js create mode 100644 ex3.js create mode 100644 ex4.js create mode 100644 ex5.js create mode 100644 ex6.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d6fdf9d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/.idea +*.iml +*.log + +node_modules diff --git a/data/db.json b/data/db.json new file mode 100644 index 0000000..9b83db7 --- /dev/null +++ b/data/db.json @@ -0,0 +1,23 @@ +{ + "persons": [ + { + "id": 2948, + "code": "A03259", + "name": "Jill", + "age": 20 + } + ], + "health-insurance": [ + { + "id": "A03259", + "isInsured": true, + "insuredTill": "01/31/2017" + } + ], + "tax-debt": [ + { + "id": "A03259", + "debt": 100 + } + ] +} diff --git a/data/routes.json b/data/routes.json new file mode 100644 index 0000000..c042bf3 --- /dev/null +++ b/data/routes.json @@ -0,0 +1,3 @@ +{ + "/api/": "/" +} \ No newline at end of file diff --git a/ex1.js b/ex1.js new file mode 100644 index 0000000..ac1a32b --- /dev/null +++ b/ex1.js @@ -0,0 +1,19 @@ +'use strict'; + +var array = [1, 2, 3, 4, 5, 6]; + +array = filter(array, function (each) { + return each % 2 === 0; +}); + +console.log('Even numbers: ' + array); + +function filter(array, predicate) { + var result = []; + for (var i = 0; i < array.length; i++) { + if (predicate(array[i])) { + result.push(array[i]); + } + } + return result; +} \ No newline at end of file diff --git a/ex2.js b/ex2.js new file mode 100644 index 0000000..9a95b14 --- /dev/null +++ b/ex2.js @@ -0,0 +1,12 @@ +'use strict'; + +var array = [1, 2, 3, 4, 5, 6]; + +array = array.filter(function (each) { + return each % 2 === 0; +}).map(function (each) { + return each * each; +}); + +console.log('Even numbers squared: ' + array); + diff --git a/ex3.js b/ex3.js new file mode 100644 index 0000000..05898c0 --- /dev/null +++ b/ex3.js @@ -0,0 +1,19 @@ +'use strict'; + +var array = [ + { id: 1, selected: false }, + { id: 2, selected: true }, + { id: 3, selected: false }, + { id: 4, selected: true }, + { id: 5, selected: true }, + { id: 6, selected: false } +]; + +array = array.filter(function (each) { + return each.selected; +}).map(function (each) { + return each.id; +}); + +console.log('Ids of selected objects: ' + array); + diff --git a/ex4.js b/ex4.js new file mode 100644 index 0000000..697cb47 --- /dev/null +++ b/ex4.js @@ -0,0 +1,20 @@ +'use strict'; + +function Person(name) { + this.name = name; +} + +Person.prototype.getName = function () { + return this.name; +}; + +Person.prototype.setName = function (name) { + return this.name = name; +}; + +var person = new Person('Jack'); +var person2 = new Person('Jack'); + +console.log(person.getName()); +console.log(Object.keys(person)); +console.log(Object.keys(person.__proto__)); \ No newline at end of file diff --git a/ex5.js b/ex5.js new file mode 100644 index 0000000..1543ded --- /dev/null +++ b/ex5.js @@ -0,0 +1,22 @@ +'use strict'; + + +factorial(5, function (res) { + console.log('res: ' + res); +}); + + + +function factorial(n, callback) { + if (n < 0) { + throw 'illegal argument' + } + + var result = 1; + while (n > 0) { + result *= n; + n--; + } + + callback(result); +} diff --git a/ex6.js b/ex6.js new file mode 100644 index 0000000..36e2388 --- /dev/null +++ b/ex6.js @@ -0,0 +1,34 @@ +'use strict'; + +var request = require("request"); + +get('/api/persons/2948', function(data) { + var code = data.code; + var name = data.name; + + get('/api/health-insurance/' + code, function(data) { + var isInsured = data.isInsured; + + get('/api/tax-debt/' + code, function(data) { + + var debt = data.debt; + + var person = { + name : name, + isInsured: isInsured, + taxDebt: debt + }; + + console.log(person); + }); + }); + +}); + + +// helper function +function get(url, cb) { + request('http://localhost:3000' + url, function(error, response, body) { + cb(JSON.parse(body)); + }); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..14f0c31 --- /dev/null +++ b/package.json @@ -0,0 +1,11 @@ +{ + "name": "i399exlang", + "version": "1.0.0", + "devDependencies": { + "json-server": "0.9.5", + "request": "^2.81.0" + }, + "scripts": { + "start": "json-server data/db.json --static ./ --routes data/routes.json" + } +}