-
Notifications
You must be signed in to change notification settings - Fork 12
Nikita Korzhavin js-1 #4
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| Function.prototype.myBind = function(context) { | ||
| if (typeof this !== 'function') { | ||
| throw new TypeError('this is not a function? so you cant use bind with it'); | ||
| } | ||
|
|
||
| var bindArgs = Array.prototype.slice.call(arguments, 1); | ||
| var emptyFunc = function() {}; | ||
| var currentThis = this; | ||
| var boundFunction = function() { | ||
| return currentThis.apply( | ||
| this instanceof emptyFunc && context ? this : context, | ||
| bindArgs.concat(Array.prototype.slice.call(arguments)), | ||
| ); | ||
| }; | ||
| emptyFunc.prototype = currentThis.prototype; | ||
| boundFunction.prototype = new emptyFunc(); | ||
| return boundFunction; | ||
| }; | ||
|
|
||
| /* | ||
| function addPropToNumber(number) { | ||
| return this.prop + number; | ||
| } | ||
| var bound = addPropToNumber.myBind({ prop: 9 }); | ||
| console.log(bound(1)); // 10 | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| function splitAndMerge(str, sp) { | ||
| return str | ||
| .split(' ') | ||
| .map(function(word) { | ||
| return word.split('').join(sp); | ||
| }) | ||
| .join(' '); | ||
| } | ||
|
|
||
| /* | ||
| console.log(splitAndMerge('My name is John', ' ')); | ||
| console.log(splitAndMerge('Hello World!', ',')); | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| function convert(obj) { | ||
| return Object.keys(obj).map(function(key) { | ||
| return [key, obj[key]]; | ||
| }); | ||
| } | ||
| /* | ||
| const guy = { name: 'Jeremy', age: 24, role: 'Software Engineer' }; | ||
| console.log(convert(guy)); | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function toCamelCase(str) { | ||
| return str | ||
| .split(/-|_/) | ||
| .map(function(word, index) { | ||
| return index !== 0 ? word.charAt(0).toUpperCase() + word.slice(1) : word; | ||
| }) | ||
| .join(''); | ||
| } | ||
| /* | ||
| console.log(toCamelCase('the-stealth-warrior')); | ||
| console.log(toCamelCase('The_Stealth_Warrior')); | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| function reverseString(sentence) { | ||
| return sentence | ||
| .split(' ') | ||
| .map(function(word) { | ||
| return word | ||
| .split('') | ||
| .reverse() | ||
| .join(''); | ||
| }) | ||
| .join(' '); | ||
| } | ||
| /* | ||
| console.log(reverseString(' A fun little challenge! ')); //" A nuf elttil !egnellahc " | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| function stringExpansion(str) { | ||
| if (str === '') { | ||
| return ''; | ||
| } | ||
| return str | ||
| .match(/^[a-z]+|\d+\w|[^\d]+/gi) | ||
| .reduce(function(acc, curr) { | ||
| if (!parseInt(curr)) { | ||
| acc.push(curr); | ||
| } else { | ||
| acc.push(curr[curr.length - 1].repeat(parseInt(curr[curr.length - 2]))); | ||
| } | ||
| return acc; | ||
| }, []) | ||
| .join(''); | ||
| } | ||
| /* | ||
| console.log(stringExpansion('3D2a5d2f')); // 'DDDaadddddff' | ||
| console.log(stringExpansion('3d332f2a')); // 'dddffaa' | ||
| console.log(stringExpansion('abcde')); // 'abcde' | ||
| console.log(stringExpansion('ab2deee2ff')); | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| function largest() { | ||
| var nums = []; | ||
| for (var i = 0; i < arguments.length; i++) { | ||
| nums[i] = arguments[i]; | ||
| } | ||
| return nums.reduce(function(acc, curr) { | ||
| return curr > acc ? curr : acc; | ||
| }, nums[0]); | ||
| } | ||
|
|
||
| function smallest() { | ||
| var nums = []; | ||
| for (var i = 0; i < arguments.length; i++) { | ||
| nums[i] = arguments[i]; | ||
| } | ||
| return nums.reduce(function(acc, curr) { | ||
| return curr < acc ? curr : acc; | ||
| }, nums[0]); | ||
| } | ||
| /* | ||
| console.log(largest(2, 0.1, -5, 100, 3)); // 100 | ||
| console.log(smallest(2, 0.1, -5, 100, 3)); // -5 | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| function transform(arr) { | ||
| return arr.map(function(num) { | ||
| return function() { | ||
| return num; | ||
| }; | ||
| }); | ||
| } | ||
| /* | ||
| const baseArray = [10, 20, 30, 40, 50]; | ||
| const newArray = transform(baseArray); | ||
|
|
||
| console.log(newArray[3]()); | ||
| console.log(newArray[4]()); | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| function sum() { | ||
| // превращаем arguments в нечто удобоваримое | ||
| var nums = []; | ||
|
|
||
| for (var i = 0; i < arguments.length; i++) { | ||
| nums[i] = arguments[i]; | ||
| } | ||
|
|
||
| var numsFlat = nums.reduce(function(acc, curr) { | ||
| return acc.concat(curr); | ||
| }, []); | ||
| // сама рекурсия | ||
| if (numsFlat.length === 1) { | ||
| return numsFlat[0]; | ||
| } | ||
|
|
||
| return numsFlat[0] + sum(numsFlat.slice(1)); | ||
| } | ||
| /* | ||
| console.log(sum(1, 3, 5, 7)); //should return 16 | ||
| */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| function countDown(num) { | ||
| for (var i = num; i >= 0; i--) { | ||
| function time(i) { | ||
|
||
| setTimeout(function() { | ||
| console.log(i); | ||
| }, 1000 * (num - i)); | ||
| } | ||
| var binded = time.bind(null, i); | ||
| binded(i); | ||
|
||
| } | ||
| } | ||
| /* | ||
| countDown(3); // 3 2 1 0 | ||
| */ | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that you don't need this check since you are changing the prototype of Function. If you will try to invoke
myBindfrom string, for instance, you'll getTypeError: myBind is not a functionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed