From 648a14ec62c314d1bc8a4725819d187527d99c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A7=D1=83=D0=B1=D0=BA=D0=BE=20=D0=9C=D0=B8=D1=85=D0=B0?= =?UTF-8?q?=D0=B9=D0=BB=D0=BE?= Date: Tue, 28 Nov 2023 18:26:24 +0200 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BA-12=5F=D0=A7=D1=83=D0=B1=D0=BA?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Exercises/1-random.js | 2 ++ Exercises/2-key.js | 9 ++++++--- Exercises/3-ip.js | 9 ++++----- Exercises/4-methods.js | 14 +++++++++++++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..ff6bfd7 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -4,6 +4,8 @@ const random = (min, max) => { // Generate random Number between from min to max // Use Math.random() and Math.floor() // See documentation at MDN + 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..aee2b70 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 key = ''; + for (let i = 0; i < length; i++) { + const randomIndex = Math.floor(Math.random() * possible.length); + key += possible[randomIndex]; + } + return key; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 1e2c406..0012439 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,10 @@ '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 + return ip.split('.').reduce((accumulator, currentValue, currentIndex, array) => { + return accumulator + (parseInt(currentValue) << ((array.length - currentIndex - 1) * 8)); + }, 0); }; module.exports = { ipToInt }; + diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..54d2668 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,7 +1,7 @@ 'use strict'; const methods = iface => { - // Introspect all properties of iface object and + // Introspect all properties of iface object and // extract function names and number of arguments // For example: { // m1: x => [x], @@ -16,6 +16,18 @@ const methods = iface => { // ['m2', 2], // ['m3', 3] // ] + const methodNames = Object.keys(iface); + const methodsArray = methodNames.reduce((acc, methodName) => { + const prop = iface[methodName]; + if (typeof prop === 'function') { + acc.push([methodName, prop.length]); + } + return acc; + }, []); + + return methodsArray; }; module.exports = { methods }; + +