FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Tasks Done by gidra5 · Pull Request #22 · HowProgrammingWorks/Function · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 5 additions & 3 deletions Exercises/1-random.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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
const fraction = Math.random();
const randomVal =
Math.floor((1 - fraction) * min + fraction * max);

return randomVal;
};

module.exports = { random };
16 changes: 13 additions & 3 deletions Exercises/2-key.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
'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 base = possible.length - 1;

while (key.length < length) {
const index = Math.floor(Math.random() * base);
key += possible[index];
}

return key;
};

module.exports = { generateKey };
13 changes: 8 additions & 5 deletions Exercises/3-ip.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'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 sitwise 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') => {
// 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 parts = ip.split('.');
return parts.reduce((acc, part) => (acc << 8) + parseInt(part), 0);
};

module.exports = { ipToInt };
39 changes: 24 additions & 15 deletions Exercises/4-methods.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
'use strict';

// 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 => {
// 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 res = [];
for (const name in iface) {
const property = iface[name];
if (typeof property === 'function')
res.push([name, property.length]);
}

return res;
};

module.exports = { methods };

Back | FazBrowse Home | New Git URL