From 289be5417b4ccae0931013115d80d7452ec39f35 Mon Sep 17 00:00:00 2001 From: Ivan Opara Date: Sun, 3 Nov 2019 17:27:12 +0300 Subject: [PATCH] all done --- Exercises/1-random.js | 6 +++--- Exercises/2-key.js | 9 ++++++--- Exercises/3-ip.js | 10 +++------- Exercises/4-methods.js | 22 +++++++--------------- 4 files changed, 19 insertions(+), 28 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..4060390 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,9 @@ '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.ceil(min); + max = Math.floor(max); + return Math.floor(Math.random() * (max - min + 1)) + min; }; module.exports = { random }; diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..e4eeba5 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,12 @@ 'use strict'; const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN + let result = ''; + while (result.length < length) { + const randomIndex = Math.floor(Math.random() * possible.length); + result += possible[randomIndex]; + } + return result; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 5b448dd..8372a64 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,7 @@ '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 = '127.0.0.1') => ip.split('.').reduce( + (sum, elem, i, arr) => sum + (Number(elem) << 8 * (arr.length - 1 - i)) + , 0); module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..d6f51c5 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 introspection = []; + for (const key in iface) { + if (typeof iface[key] === 'function') { + introspection.push([key, iface[key].length]); + } + } + return introspection; }; module.exports = { methods };