diff --git a/.eslintrc.json b/.eslintrc.json index 188baf7..9b0f412 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,7 +18,7 @@ ], "linebreak-style": [ "error", - "unix" + "windows" ], "quotes": [ "error", @@ -265,4 +265,4 @@ "never" ] } -} +} \ No newline at end of file diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..457e0b5 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,12 @@ '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) + min); }; module.exports = { random }; diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..f075d2b 100644 --- a/Exercises/2-key.js +++ b/Exercises/2-key.js @@ -1,9 +1,14 @@ 'use strict'; const generateKey = (length, possible) => { - // Generate string of random characters - // Use Math.random() and Math.floor() - // See documentation at MDN + let result = ''; + + for (let i = 0; i < length; i++) { + const ranIdx = Math.floor(Math.random() * possible.length); + result += possible[ranIdx]; + } + + return result; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 1e2c406..9b3bd4e 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,8 @@ '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 ipSplit = ip.split('.'); + return ipSplit.reduce((acc, val) => (acc << 8) + parseInt(val), 0); }; module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..8811d2f 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,16 @@ '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 collection = []; + + for (const key in iface) { + collection.push([ + iface[key].name, + iface[key].length + ]); + } + + return collection; }; module.exports = { methods };