From 78f699d1dc1c8c274810c28affa9f85239c75376 Mon Sep 17 00:00:00 2001 From: hok7z Date: Mon, 2 Dec 2024 21:32:29 +0200 Subject: [PATCH] First commit --- Exercises/1-random.js | 14 ++++++++++---- Exercises/2-key.js | 14 +++++++++++--- Exercises/3-ip.js | 32 +++++++++++++++++++++++++++----- Exercises/4-methods.js | 41 +++++++++++++++++++++++++---------------- 4 files changed, 73 insertions(+), 28 deletions(-) diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..1223225 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,15 @@ '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 -}; + if (max === undefined) { + max = min; + min = 0; + } + 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..e7c62fc 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,17 @@ '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 max = possible.length; + for (let i = 0; i < length; i++) { + const randomIndex = Math.floor(Math.random() * max); + key += possible[randomIndex]; + } + return key; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 1e2c406..7701fa8 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,33 @@ '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 bitwise shift +// (10 << 8 << 8 << 8) + (0 << 8 << 8) + (0 << 8) + 1 === 167772161 +// Use Array.prototype.reduce of for loop + +// Implementation ipToInt using loop +// const ipToInt1 = (ip = '127.0.0.1') => { +// const ipArray = ip.split('.'); +// let result = 0, result1; +// let count = 3; +// +// for (const item of ipArray) { +// result1 = parseInt(item); +// for (let i = 1; i <= count; i++) { +// result1 <<= 8; +// } +// count--; +// result += result1; +// } +// +// return result; +// }; + +// Implementation ipToInt using Array.prototype.reduce 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 + const ipArray = ip.split('.'); + return ipArray.reduce((res, curValue) => (res << 8) + parseInt(curValue), 0); }; module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index 75ca8df..5972d30 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,30 @@ '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] - // ] +// 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) => { + const data = []; + + for (const key in iface) { + const propertie = iface[key]; + data.push([propertie.name, propertie.length]); + } + + return data; }; module.exports = { methods };