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

Labs Function by Vlad Golik by VladyslavHolik · Pull Request #30 · 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) .test  (3) All 2 file types 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
7 changes: 2 additions & 5 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,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 };
2 changes: 1 addition & 1 deletion Exercises/1-random.test
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,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');
Expand Down
15 changes: 10 additions & 5 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,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 };
16 changes: 10 additions & 6 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,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 };
2 changes: 1 addition & 1 deletion Exercises/3-ip.test
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,6 +1,6 @@
({
name: 'ipToInt',
length: [60, 150],
length: [60, 270],
cases: [
['127.0.0.1', 2130706433],
['10.0.0.1', 167772161],
Expand Down
25 changes: 9 additions & 16 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,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 };
2 changes: 1 addition & 1 deletion Exercises/4-methods.test
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,6 +1,6 @@
({
name: 'methods',
length: [180, 220],
length: [20, 220],
cases: [
[
{
Expand Down

Back | FazBrowse Home | New Git URL