diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..6653494 100644 --- a/Exercises/1-random.js +++ b/Exercises/1-random.js @@ -1,9 +1,6 @@ 'use strict'; -const random = (min, max) => { - // Generate random Number between from min to max - // Use Math.random() and Math.floor() - // See documentation at MDN -}; +const random = (min = 0, max) => Math.floor(Math.random() * (max - min + 1) + min); + module.exports = { random }; diff --git a/Exercises/1-random.test b/Exercises/1-random.test index 15abbf9..7e5356c 100644 --- a/Exercises/1-random.test +++ b/Exercises/1-random.test @@ -1,6 +1,6 @@ ({ name: 'random', - length: [120, 150], + length: [20, 150], test: random => { const x = random(0, 10); if (typeof x !== 'number') throw new Error('Expected number result'); diff --git a/Exercises/2-key.js b/Exercises/2-key.js index ba7e53a..8065a78 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 +const generateKey = (leng, char) => { + let res = ''; + let i = 0; + while (i < leng) { + let ba = char.length -1; + const ran = Math.floor(Math.random() * (ba + 1)); + res += char[ran]; + i++; + } + return res; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 5b448dd..0a596c4 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,15 @@ '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 sitwise shift - // (10 << 8 << 8 << 8) + (0 << 8 << 8) + (0 << 8) + 1 === 167772161 - // Use Array.prototype.reduce of for loop +const ipToInt = (str = '127.0.0.1') => { + const a = str.split('.'); + for (const i in a) { + a[i] = +a[i]; + } + for (let i = 3, j = 0; i >= 0; i--, j++) { + a[j] = a[j] << 8 * i; + } + return a + .reduce((sum, cur) => sum + cur, 0); }; module.exports = { ipToInt }; diff --git a/Exercises/3-ip.test b/Exercises/3-ip.test index 6fb967b..27dc79f 100644 --- a/Exercises/3-ip.test +++ b/Exercises/3-ip.test @@ -1,6 +1,6 @@ ({ name: 'ipToInt', - length: [60, 150], + length: [60, 270], cases: [ ['127.0.0.1', 2130706433], ['10.0.0.1', 167772161], diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..2c7bd59 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 = obj => { + const ans = []; + for (const key in obj) { + if (typeof obj[key] === 'function') { + ans + .push([key, obj[key].length]); + } + } + return ans; }; module.exports = { methods }; diff --git a/Exercises/4-methods.test b/Exercises/4-methods.test index a408b22..989e1a9 100644 --- a/Exercises/4-methods.test +++ b/Exercises/4-methods.test @@ -1,6 +1,6 @@ ({ name: 'methods', - length: [180, 220], + length: [20, 220], cases: [ [ {