Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Lab3 Function #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 2 .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"linebreak-style": [
"error",
"unix"
"windows"
],
"quotes": [
"error",
Expand Down
10 changes: 6 additions & 4 deletions 10 Exercises/1-random.js
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
if (max === undefined) {
max = min;
min = 0;
}
return Math.floor(Math.random() * (max - min + 1)) + min;
};

module.exports = { random };

13 changes: 8 additions & 5 deletions 13 Exercises/2-key.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

const generateKey = (length, possible) => {
// Generate string of random characters
// Use Math.random() and Math.floor()
// See documentation at MDN
const generateKey = (length, characters) => {
const len = characters.length;
let res = '';
for (let i = 0; i < length; i++) {
const position = Math.floor(Math.random() * len);
res += characters[position];
}
return res;
};

module.exports = { generateKey };
9 changes: 3 additions & 6 deletions 9 Exercises/3-ip.js
Original file line number Diff line number Diff line change
@@ -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 ipNumber = ip.split('.')
.reduce((sum, cur) => (sum << 8) + parseInt(cur));
return ipNumber;
};

module.exports = { ipToInt };
37 changes: 20 additions & 17 deletions 37 Exercises/4-methods.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
'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 res = [];
for (const name in iface) {
const func = iface[name];
if (typeof func === 'function') {
res.push([func.name, func.length]);
}
}
return res;
};

const obj = {
m1: (x) => [x],
m2(x, y) {
return [x, y];
},
m3(x, y, z) {
return [x, y, z];
}
};
const res = methods(obj);
console.log(res);
module.exports = { methods };
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.