From 7bba39f7a8f76a9188365d921f4da56b25946c53 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 21 Oct 2019 20:08:23 +0300 Subject: [PATCH 1/2] Tasks done --- Exercises/1-random.js | 8 +++++--- Exercises/2-key.js | 16 +++++++++++++--- Exercises/3-ip.js | 13 ++++++++----- Exercises/4-methods.js | 40 +++++++++++++++++++++++++--------------- 4 files changed, 51 insertions(+), 26 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..d3ea6be 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 + const fraction = Math.random(); + const randomVal = + Math.floor((1 - fraction) * min + fraction * max); + + return randomVal; }; module.exports = { random }; diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..2d4a184 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,19 @@ 'use strict'; +// Generate string of random characters +// Use Math.random() and Math.floor() +// See documentation at MDN + const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN + let key = ''; + const base = possible.length - 1; + + while (key.length < length) { + const index = Math.floor(Math.random() * base); + key += possible[index]; + } + + return key; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 5b448dd..6ac7e25 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +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 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 parts = ip.split('.'); + return parts.reduce((acc, part) => (acc << 8) + parseInt(part), 0); }; module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..8c88396 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,31 @@ 'use strict'; +// 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 => { - // 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] - // ] + let res = []; + for (const name in iface) + { + const property = iface[name]; + if (typeof property === 'function') + res.push([name, property.length]); + } + + return res; }; module.exports = { methods }; From cabf6522c7237f622ed4ea2421292f875216706b Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 21 Oct 2019 20:11:46 +0300 Subject: [PATCH 2/2] eslint fix --- Exercises/4-methods.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index 8c88396..7f6e814 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -17,9 +17,8 @@ // ] const methods = iface => { - let res = []; - for (const name in iface) - { + const res = []; + for (const name in iface) { const property = iface[name]; if (typeof property === 'function') res.push([name, property.length]);