done state

This commit is contained in:
Märt Kalmo 2017-03-30 13:59:37 +03:00
commit 2056262a00
10 changed files with 168 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/.idea
*.iml
*.log
node_modules

23
data/db.json Normal file
View File

@ -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
}
]
}

3
data/routes.json Normal file
View File

@ -0,0 +1,3 @@
{
"/api/": "/"
}

19
ex1.js Normal file
View File

@ -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;
}

12
ex2.js Normal file
View File

@ -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);

19
ex3.js Normal file
View File

@ -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);

20
ex4.js Normal file
View File

@ -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__));

22
ex5.js Normal file
View File

@ -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);
}

34
ex6.js Normal file
View File

@ -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));
});
}

11
package.json Normal file
View File

@ -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"
}
}