From 26eff8d3b1d54f59dc0738e2786f4237b9fd64ea Mon Sep 17 00:00:00 2001 From: Vladlinu Date: Mon, 25 Nov 2019 20:31:39 +0200 Subject: [PATCH 1/2] Labs Function by Vlad Golik --- Exercises/1-random.js | 7 ++----- Exercises/1-random.test | 2 +- Exercises/2-key.js | 15 ++++++++++----- Exercises/2-key.test | 2 +- Exercises/3-ip.js | 17 +++++++++++------ Exercises/3-ip.test | 2 +- Exercises/4-methods.js | 24 ++++++++---------------- Exercises/4-methods.test | 2 +- 8 files changed, 35 insertions(+), 36 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..574f8b6 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,6 @@ 'use strict'; -const random = (min, max) => { - // Generate random Number between from min to max - // Use Math.random() and Math.floor() - // See documentation at MDN -}; +const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); + module.exports = { random }; diff --git a/Exercises/1-random.test b/Exercises/1-random.test index 15abbf9..7e5356c 100644 --- a/Exercises/1-random.test +++ b/Exercises/1-random.test @@ -1,6 +1,6 @@ ({ name: 'random', - length: [120, 150], + length: [20, 150], test: random => { const x = random(0, 10); if (typeof x !== 'number') throw new Error('Expected number result'); diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..087c813 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,14 @@ 'use strict'; - -const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN +const generateKey = (leng, char) => { + const rand = (a, b) => Math.floor(Math.random() * (b - a + 1) + a); + let res = ''; + let i = 0; + while (i < leng) { + const ran = rand(0, char.length - 1); + res += char[ran]; + i++; + } + return res; }; module.exports = { generateKey }; diff --git a/Exercises/2-key.test b/Exercises/2-key.test index 6a368e7..70abab7 100644 --- a/Exercises/2-key.test +++ b/Exercises/2-key.test @@ -1,6 +1,6 @@ ({ name: 'generateKey', - length: [200, 210], + length: [20, 250], test: generateKey => { const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; const key = generateKey(16, characters); diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 5b448dd..55962e0 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,16 @@ 'use strict'; -const ipToInt = (ip = '127.0.0.1') => { - // Parse ip address as string, for example '10.0.0.1' - // to ['10', '0', '0', '1'] to [10, 0, 0, 1] - // and convert to Number value 167772161 with sitwise shift - // (10 << 8 << 8 << 8) + (0 << 8 << 8) + (0 << 8) + 1 === 167772161 - // Use Array.prototype.reduce of for loop +const ipToInt = (str = '127.0.0.1') => { + const arr = str.split('.'); + for (const i in arr) { + arr[i] = +arr[i]; + } + for (let i = 3, j = 0; i >= 0; i--, j++) { + arr[j] = arr[j] << 8 * i; + } + const res = arr + .reduce((sum, cur) => sum + cur, 0); + return res; }; module.exports = { ipToInt }; diff --git a/Exercises/3-ip.test b/Exercises/3-ip.test index 6fb967b..27dc79f 100644 --- a/Exercises/3-ip.test +++ b/Exercises/3-ip.test @@ -1,6 +1,6 @@ ({ name: 'ipToInt', - length: [60, 150], + length: [60, 270], cases: [ ['127.0.0.1', 2130706433], ['10.0.0.1', 167772161], diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..127a1a4 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,13 @@ 'use strict'; -const methods = iface => { - // Introspect all properties of iface object and - // extract function names and number of arguments - // For example: { - // m1: x => [x], - // m2: function (x, y) { - // return [x, y]; - // }, - // m3(x, y, z) { - // return [x, y, z]; - // } - // will return: [ - // ['m1', 1], - // ['m2', 2], - // ['m3', 3] - // ] +const methods = obj => { + const ans = []; + for (const key in obj) { + if (typeof obj[key] === 'function') { + ans.push([key, obj[key].length]); + } + } + return ans; }; module.exports = { methods }; diff --git a/Exercises/4-methods.test b/Exercises/4-methods.test index a408b22..989e1a9 100644 --- a/Exercises/4-methods.test +++ b/Exercises/4-methods.test @@ -1,6 +1,6 @@ ({ name: 'methods', - length: [180, 220], + length: [20, 220], cases: [ [ { From 083a65f00854b9e055a8d4d0ab7783f89e82fdd1 Mon Sep 17 00:00:00 2001 From: Vladlinu Date: Mon, 25 Nov 2019 20:48:21 +0200 Subject: [PATCH 2/2] Labs Function by Vlad Golik --- Exercises/1-random.js | 2 +- Exercises/2-key.js | 4 ++-- Exercises/2-key.test | 2 +- Exercises/3-ip.js | 11 +++++------ Exercises/4-methods.js | 3 ++- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index 574f8b6..6653494 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,6 +1,6 @@ 'use strict'; -const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min); +const random = (min = 0, max) => Math.floor(Math.random() * (max - min + 1) + min); module.exports = { random }; diff --git a/Exercises/2-key.js b/Exercises/2-key.js index 087c813..8065a78 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,10 +1,10 @@ 'use strict'; const generateKey = (leng, char) => { - const rand = (a, b) => Math.floor(Math.random() * (b - a + 1) + a); let res = ''; let i = 0; while (i < leng) { - const ran = rand(0, char.length - 1); + let ba = char.length -1; + const ran = Math.floor(Math.random() * (ba + 1)); res += char[ran]; i++; } diff --git a/Exercises/2-key.test b/Exercises/2-key.test index 70abab7..6a368e7 100644 --- a/Exercises/2-key.test +++ b/Exercises/2-key.test @@ -1,6 +1,6 @@ ({ name: 'generateKey', - length: [20, 250], + length: [200, 210], test: generateKey => { const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; const key = generateKey(16, characters); diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 55962e0..0a596c4 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,16 +1,15 @@ 'use strict'; const ipToInt = (str = '127.0.0.1') => { - const arr = str.split('.'); - for (const i in arr) { - arr[i] = +arr[i]; + const a = str.split('.'); + for (const i in a) { + a[i] = +a[i]; } for (let i = 3, j = 0; i >= 0; i--, j++) { - arr[j] = arr[j] << 8 * i; + a[j] = a[j] << 8 * i; } - const res = arr + return a .reduce((sum, cur) => sum + cur, 0); - return res; }; module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index 127a1a4..2c7bd59 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -4,7 +4,8 @@ const methods = obj => { const ans = []; for (const key in obj) { if (typeof obj[key] === 'function') { - ans.push([key, obj[key].length]); + ans + .push([key, obj[key].length]); } } return ans;