diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..f9839b1 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 + 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..01c6232 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,27 @@ 'use strict'; +const characters = () => { + // some text about function + const temp = []; + for (let i = 48; i < 91; i++) { + if (i > 57 && i < 65) continue; + temp.push(String.fromCharCode(i).toLowerCase()); + } + return temp; +}; + const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN + // some text about function + let key = ''; + for (let i = 0; i < length; i++) { + const random = Math.floor(Math.random() * 25) + 1; + key += possible[random]; + } + return key; }; +const chars = characters(); +const key = generateKey(16, chars); +console.log(key); + module.exports = { generateKey }; diff --git a/Exercises/3-ip copy.js b/Exercises/3-ip copy.js new file mode 100644 index 0000000..3017365 --- /dev/null +++ b/Exercises/3-ip copy.js @@ -0,0 +1,33 @@ +'use strict'; + +const ipToInt = (ip = '127.0.0.1') => { + + const stringToArray = ip.split('.'); + const stringToNumbers = []; + + stringToArray.forEach((el) => { + stringToNumbers.push(Number(el)); + }); + + const resultArray = []; + const sideElements = stringToNumbers.reverse(); + + while (sideElements.length) { + + let sideElement = sideElements.pop(); + let i = sideElements.length; + + while (i > 0) { + sideElement <<= 8; + i--; + } + + resultArray.push(sideElement); + + } + + return resultArray.reduce((acc, vol) => (acc += vol)); + +}; + +module.exports = { ipToInt }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 1e2c406..89c8839 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,9 @@ '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 +const ipToInt = (ip = '10.0.0.1') => { + const callback = (acc, item) => (acc << 8) + parseInt(item); + const out = ip.split('.').reduce(callback, 0); + return out; }; module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index 75ca8df..0ae737e 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,14 @@ '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 methods = (iface) => { + + const resultArray = []; + + for (const fn of Object.keys(iface)) { + const length = iface[fn].length; //todo checking typeof function + resultArray.push([fn, length]); + } + return resultArray; }; module.exports = { methods }; diff --git a/Solutions/3-ip.js b/Solutions/3-ip.js index 206c406..8657649 100644 --- a/Solutions/3-ip.js +++ b/Solutions/3-ip.js @@ -5,4 +5,11 @@ const ipToInt = (ip = '127.0.0.1') => { return ip.split('.').reduce(fn, 0); }; +ipToInt(); + module.exports = { ipToInt }; + + +const ip = '127.0.0.1'; + +//console.log(ip.split('.')); \ No newline at end of file