From f010a60456a24a6963bd469682040212296d5a7e Mon Sep 17 00:00:00 2001 From: OleksandrLevak Date: Sun, 20 Oct 2019 17:40:48 +0300 Subject: [PATCH 1/3] Labs tasks 1-4 --- Exercises/1-random.js | 7 ++++--- Exercises/2-key.js | 10 +++++++--- Exercises/3-ip.js | 10 ++++------ Exercises/4-methods.js | 23 ++++++++--------------- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..f2ce13b 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,10 @@ 'use strict'; const random = (min, max) => { - // Generate random Number between from min to max - // Use Math.random() and Math.floor() - // See documentation at MDN + //min = Math.floor(min); + //max = Math.floor(max); + const res = Math.floor(Math.random() * (max - min) + min); + return res; }; module.exports = { random }; diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..42b4f84 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,13 @@ 'use strict'; const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN + const arr = possible; + let string = ''; + for (let i = 0; i < length; i++) { + const num = Math.floor(Math.random() * length); + string += arr[num]; + } + return string; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 5b448dd..0256609 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,9 @@ '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 = ip => { + const a = ip.split('.').map(x => parseInt(x, 10)) + .reduce((m, n) => (m << 8) + n); + return a; }; module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..e5d863c 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,14 @@ '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 arr = []; + for (const i in iface) { + const fn = iface[i].length; + //const name = iface[i]; + /*if (typeof name === 'function')*/ + arr.push([i, fn]); + } + return arr; }; module.exports = { methods }; From dead9bfd38e32b0bcf520db979a18b460e685ac0 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Fri, 15 Nov 2019 17:50:56 +0200 Subject: [PATCH 2/3] test --- Exercises/1-random.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index f2ce13b..c71e004 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,8 +1,8 @@ 'use strict'; const random = (min, max) => { - //min = Math.floor(min); - //max = Math.floor(max); + /// min = Math.floor(min); + /// max = Math.floor(max); const res = Math.floor(Math.random() * (max - min) + min); return res; }; From 6fc4774e259779f7a33282e6a616e999d42c73a5 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Fri, 15 Nov 2019 17:52:08 +0200 Subject: [PATCH 3/3] Labs --- Exercises/1-random.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index c71e004..0f9e5e9 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,8 +1,8 @@ 'use strict'; const random = (min, max) => { - /// min = Math.floor(min); - /// max = Math.floor(max); + // min = Math.floor(min); + // max = Math.floor(max); const res = Math.floor(Math.random() * (max - min) + min); return res; };