Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions Nikita_Korzhavin/js10myBind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Function.prototype.myBind = function(context) {
if (typeof this !== 'function') {
Copy link

@svvald svvald Aug 6, 2019

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 myBind from string, for instance, you'll get TypeError: myBind is not a function

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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
*/
13 changes: 13 additions & 0 deletions Nikita_Korzhavin/js1splitAndMerge.js
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!', ','));
*/
9 changes: 9 additions & 0 deletions Nikita_Korzhavin/js2convert.js
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));
*/
12 changes: 12 additions & 0 deletions Nikita_Korzhavin/js3ToCamelCase.js
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'));
*/
14 changes: 14 additions & 0 deletions Nikita_Korzhavin/js4reverceString.js
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 "
*/
22 changes: 22 additions & 0 deletions Nikita_Korzhavin/js5stringExpansion.js
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'));
*/
23 changes: 23 additions & 0 deletions Nikita_Korzhavin/js6largestSmallest.js
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
*/
14 changes: 14 additions & 0 deletions Nikita_Korzhavin/js7transform.js
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]());
*/
21 changes: 21 additions & 0 deletions Nikita_Korzhavin/js8sum.js
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
*/
14 changes: 14 additions & 0 deletions Nikita_Korzhavin/js9countDown.js
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) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move function declaration outside of for loop

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

setTimeout(function() {
console.log(i);
}, 1000 * (num - i));
}
var binded = time.bind(null, i);
binded(i);
Copy link

@svvald svvald Aug 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write it like this time.bind(null, i)() or even better time.bind(null)(i) which is called currying.
Although I don't insist on changing it but strongly recommend you to read more about it since it is a part of functional approach and some JS programmers prefer it to imperative nowadays

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
}
/*
countDown(3); // 3 2 1 0
*/