From 5283aca7440862ac9cb117fa09047f9521129fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B0=D0=BD=D1=96=D1=81=D0=BB=D0=B0=D0=B2=20?= =?UTF-8?q?=D0=9A=D1=83=D0=BB=D1=96=D1=87=D0=BA=D0=BE=D0=B2=D1=81=D1=8C?= =?UTF-8?q?=D0=BA=D0=B8=D0=B9?= Date: Sat, 19 Oct 2019 15:15:42 +0300 Subject: [PATCH] Add Homework of Function --- Exercises/1-random.js | 11 ++++++++--- Exercises/2-key.js | 13 ++++++++++--- Exercises/3-ip.js | 17 ++++++++++++----- Exercises/4-methods.js | 38 +++++++++++++++++++++++--------------- 4 files changed, 53 insertions(+), 26 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..529fd5c 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,14 @@ 'use strict'; +// Generate random Number between from min to max +// Use Math.random() and Math.floor() +// See documentation at MDN + const random = (min, max) => { - // Generate random Number between from min to max - // Use Math.random() and Math.floor() - // See documentation at MDN + const rand = Math.random(); + const value = rand * (max - min + 1) + min; + const result = Math.floor(value); + return result; }; module.exports = { random }; diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..99a76fa 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,16 @@ '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 res = ''; + for (let i = 0; i < length; i++) { + const ran = Math.floor(Math.random() * possible.length); + const ch = possible[ran]; + res += ch; + } + return res; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 5b448dd..219f9d5 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,18 @@ '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 ar = ip.split('.'); + let res = 0; + for (let i = 0; i < ar.length; i++) res = (res << 8) + ar[i] * 1; + return res; }; + module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..8568b46 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,29 @@ '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] - // ] + const result = []; + for (const name in iface) { + if (typeof iface[name] === 'function') { + result.push([name, iface[name].length]); + } + } + return result; }; module.exports = { methods };