diff --git a/Exercises/.prettierrc b/Exercises/.prettierrc new file mode 100644 index 0000000..4cb85b8 --- /dev/null +++ b/Exercises/.prettierrc @@ -0,0 +1,7 @@ +{ + "trailingComma": "es5", + "tabWidth": 2, + "semi": true, + "singleQuote": true, + "space-before-function-paren": ["error", "never"] +} diff --git a/Exercises/1-random.js b/Exercises/1-random.js index ef5ccaf..15334bb 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 +const random = (min, max = null) => { + if (!max) { + 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..6ecdde6 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 randomIndex = Math.floor(Math.random() * possible.length); + result += possible[randomIndex]; + } + + return result; }; module.exports = { generateKey }; diff --git a/Exercises/3-ip.js b/Exercises/3-ip.js index 1e2c406..441ea5c 100644 --- a/Exercises/3-ip.js +++ b/Exercises/3-ip.js @@ -1,11 +1,11 @@ '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 = '127.0.0.1') => + ip + .split('.') + .reduce( + (result, current, index) => result + (+current << ((3 - index) * 8)), + 0 + ); module.exports = { ipToInt }; diff --git a/Exercises/4-methods.js b/Exercises/4-methods.js index c1038e8..a3e80c1 100644 --- a/Exercises/4-methods.js +++ b/Exercises/4-methods.js @@ -1,21 +1,40 @@ '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 result = []; + for (const method in iface) { + if (typeof iface[method] === 'function') { + result.push([method, iface[method].length]); + } + } + + return result; }; +// console.log( +// 'methods :>> ', +// methods({ +// m1: (x) => [x], +// m2: function (x, y) { +// return [x, y]; +// }, +// m3(x, y, z) { +// return [x, y, z]; +// }, +// }) +// ); + +// function inspectInterface(iface) { +// // Get the keys of iface object +// let keys = Object.keys(iface); + +// // Filter keys with function type +// keys = keys.filter((key) => typeof iface[key] === 'function'); + +// // Map the keys array to a desired format +// let result = keys.map((key) => [key, iface[key].length]); + +// return result; +// } + module.exports = { methods }; diff --git a/JavaScript/1-simple.js b/JavaScript/1-simple.js index b791331..4aa42dc 100644 --- a/JavaScript/1-simple.js +++ b/JavaScript/1-simple.js @@ -1,10 +1,10 @@ -'use strict'; +"use strict"; function inc(a) { return a + 1; } -const sum = function(a, b) { +const sum = function (a, b) { return a + b; }; @@ -15,7 +15,14 @@ const avg = (a, b) => { return s / 2; }; -console.log('inc(5) = ' + inc(5)); -console.log('sum(1, 3) = ' + sum(1, 3)); -console.log('max(8, 6) = ' + max(8, 6)); -console.log('avg(8, 6) = ' + avg(8, 6)); +console.log("inc(5) = " + inc(5)); +console.log("sum(1, 3) = " + sum(1, 3)); +console.log("max(8, 6) = " + max(8, 6)); +console.log("avg(8, 6) = " + avg(8, 6)); + +function ipToInt(ip = "127.0.0.1") { + return ip + .split(".") // превращаем строку в массив, разбивая её на элементы по '.' + .map((byte, index) => Number(byte) << ((3 - index) * 8)) // сдвигаем каждый байт на нужное количество битов в соответствии с правилами, указанными выше + .reduce((accum, current) => accum + current, 0); // суммируем все, чтобы получить окончательное число +} diff --git a/package.json b/package.json index ebfc0c9..c4de81a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "author": "Timur Shemsedinov ", "license": "MIT", "scripts": { - "test": "eslint ./Exercises; hpw", + "test": "eslint ./Exercises && hpw", "ci": "eslint ./Exercises && hpw" }, "dependencies": {