From 8bd8d13619ac0ff1d1e437528637a5b30892bb43 Mon Sep 17 00:00:00 2001 From: Ovsky Aleksander Date: Wed, 30 Oct 2019 17:04:17 +0200 Subject: [PATCH 1/2] Short --- Exercises/1-random.js | 10 ++++++---- Exercises/2-key-short.js | 10 ++++++++++ Exercises/2-key.js | 11 +++++++---- Exercises/3-ip.js | 9 ++++++--- 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 Exercises/2-key-short.js diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..9c7e590 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,11 @@ 'use strict'; - +const { random: frandom, floor: trunc } = Math; const random = (min, max) => { - // Generate random Number between from min to max - // Use Math.random() and Math.floor() - // See documentation at MDN + if (typeof(max) === 'undefined') { + max = min; + min = 0; + } + return trunc((max - min + 1) * frandom() + min); }; module.exports = { random }; diff --git a/Exercises/2-key-short.js b/Exercises/2-key-short.js new file mode 100644 index 0000000..cc33bde --- /dev/null +++ b/Exercises/2-key-short.js @@ -0,0 +1,10 @@ +'use strict'; +const generateKey = (length, possible) => { + let s = ''; + for (let i = 0; i < length; i++) { + s += possible[Math.floor(Math.random() * length)]; + } + return s; +}; + +module.exports = { generateKey }; diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..a752487 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,12 @@ 'use strict'; - +const { random: frandom, floor: trunc } = Math; const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN + let s = ''; + const plength = possible.length; + for (let i = 0; i < length; i++) { + s += possible[trunc(frandom() * plength)]; + } + return s; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 5b448dd..8ee5182 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,14 @@ '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 intNumber = str => parseInt(str,10); +const ipToInt = (ip = '127.0.0.1') => { + const arr = ip.split(',').map(intNumber); + return arr; }; -module.exports = { ipToInt }; +console.div(ipToInt()); +//module.exports = { ipToInt }; From aa2f138c5ad2e7b5e676fb0babf3a8a7188c632e Mon Sep 17 00:00:00 2001 From: Guyver Date: Sat, 2 Nov 2019 12:43:26 +0200 Subject: [PATCH 2/2] Labs 1-4 Lab 2. Solution is too short. But I think, that I wrote it good Kab 4. Solution is too short. I wrote it without semicolon {}. I know this from C++. When operator one semicolons my not used. If more then one, it is a block. I use semicolons. I used it on Java, C++ and want use in JS. --- Exercises/3-ip.js | 22 +++++++++++----------- Exercises/4-methods.js | 19 ++++--------------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 8ee5182..287446c 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,14 +1,14 @@ 'use strict'; - // 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 intNumber = str => parseInt(str,10); -const ipToInt = (ip = '127.0.0.1') => { - const arr = ip.split(',').map(intNumber); - return arr; +const intNumber = str => parseInt(str, 10); +const shiftEight = (num, index, array) => { + for (let i = 1; i < array.length - index; i++) + num <<= 8; + return num; }; +const sumArray = (sum, current) => sum + current; +const ipToInt = (ip = '127.0.0.1') => ip.split('.') + .map(intNumber) + .map(shiftEight) + .reduce(sumArray); -console.div(ipToInt()); -//module.exports = { ipToInt }; +module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..3e11dbd 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,10 @@ '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 array = []; + for (const key in iface) + if (key === 'function') array.push([key, iface[key].length]); + return array; }; module.exports = { methods };