From 82265e027a77ce405192c7a713eeeba020c121bd Mon Sep 17 00:00:00 2001 From: Orbis Date: Thu, 30 Dec 2021 16:11:59 +0300 Subject: [PATCH 1/7] homework-1 --- Exercises/1-random.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..c695af7 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,11 @@ 'use strict'; const random = (min, max) => { - // Generate random Number between from min to max - // Use Math.random() and Math.floor() - // See documentation at MDN + if (max === undefined) { + max = Math.floor(min); + min = 0; + } + return Math.floor(Math.random() * (max - min)) + min; }; module.exports = { random }; From 9123f0804b82856aa33b90904f508a05153b417c Mon Sep 17 00:00:00 2001 From: Orbis Date: Thu, 30 Dec 2021 16:12:19 +0300 Subject: [PATCH 2/7] homework-2-v1 --- Exercises/2-key.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..0e54f48 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,19 @@ 'use strict'; const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN + const res = new Array(length).fill(0); + + const f = () => { + const i = Math.floor(Math.random() * possible.length); + return possible[i]; + }; + + return res.map(f).join(''); }; +const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; +const key = generateKey(16, characters); +console.log('result:', key); // eg599gb60q926j8i +console.log(generateKey.toString().length + ' vs 210'); + module.exports = { generateKey }; From 9e45b89c23d693d03ae9eb33fe855819ef6dd2c6 Mon Sep 17 00:00:00 2001 From: Orbis Date: Thu, 30 Dec 2021 16:16:53 +0300 Subject: [PATCH 3/7] homework-2-v2 --- Exercises/2-key.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Exercises/2-key.js b/Exercises/2-key.js index 0e54f48..3fd1d64 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,14 +1,14 @@ 'use strict'; const generateKey = (length, possible) => { - const res = new Array(length).fill(0); - - const f = () => { - const i = Math.floor(Math.random() * possible.length); - return possible[i]; - }; - - return res.map(f).join(''); + let result = ''; + for (let i = 0; i < length; i++) { + const randi = Math.floor( + Math.random() * possible.length + ); + result += possible[randi]; + } + return result; }; const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; From da535b5869ab51d5ff9a8231b7c0aca411b76c2b Mon Sep 17 00:00:00 2001 From: Orbis Date: Thu, 30 Dec 2021 22:41:00 +0300 Subject: [PATCH 4/7] homework-3-v1 --- Exercises/3-ip.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 1e2c406..304f178 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,20 @@ '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 bitwise shift - // (10 << 8 << 8 << 8) + (0 << 8 << 8) + (0 << 8) + 1 === 167772161 - // Use Array.prototype.reduce of for loop + const bitShift = (acc, num, index) => { + + for (let i = 0; i < 3 - index; i++) { + console.log({ acc, num, index, i, str: `${num} << 8` }); + num = +num << 8; + } + return acc + +num; + }; + + return ip.split('.').reduce(bitShift, 0); }; +console.log(ipToInt() === 2130706433); +const l = ipToInt.toString().length; +console.log(`${l} in range [60, 150]?`, l >= 60 && l <= 150); + module.exports = { ipToInt }; From a765248954d3ac38425155525180ad280d199194 Mon Sep 17 00:00:00 2001 From: Orbis Date: Thu, 30 Dec 2021 22:53:01 +0300 Subject: [PATCH 5/7] homework-3-v2 --- Exercises/3-ip.js | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 304f178..de2d1fc 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,15 +1,7 @@ 'use strict'; const ipToInt = (ip = '127.0.0.1') => { - const bitShift = (acc, num, index) => { - - for (let i = 0; i < 3 - index; i++) { - console.log({ acc, num, index, i, str: `${num} << 8` }); - num = +num << 8; - } - return acc + +num; - }; - + const bitShift = (acc, num) => (acc << 8) + +num; return ip.split('.').reduce(bitShift, 0); }; From eb60fdb6a256a92d2319dceff67845bb3fd8b64d Mon Sep 17 00:00:00 2001 From: Orbis Date: Fri, 31 Dec 2021 00:00:49 +0300 Subject: [PATCH 6/7] Solution is too short: Length: 110 --- Exercises/4-methods.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..427bb89 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,7 @@ '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 = (iface) => Object.values(iface) + .filter((v) => typeof v === 'function') + .map((fn) => [fn.name, fn.length]); module.exports = { methods }; From 5932034dffb59b773a9b2aa338624c3bdcb9078c Mon Sep 17 00:00:00 2001 From: Orbis Date: Fri, 31 Dec 2021 00:12:33 +0300 Subject: [PATCH 7/7] longer solution --- Exercises/4-methods.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index 427bb89..5498b6a 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,7 +1,10 @@ 'use strict'; -const methods = (iface) => Object.values(iface) - .filter((v) => typeof v === 'function') - .map((fn) => [fn.name, fn.length]); +const methods = (iface) => { + const functions = Object.values(iface) + .filter((value) => typeof value === 'function'); + if (functions.length === 0) return; + return functions.map((fn) => [fn.name, fn.length]); +}; module.exports = { methods };