This file contains 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
const getElementFromTemplate = (template) => { | |
const container = document.createElement('template'); | |
container.innerHTML = template; | |
// В этом месте функция неявно вернёт DocumentFragment, | |
// который при добавлении в DOM исчезает, а вместо него | |
// вставляются его дети. | |
return container.content; | |
} |
This file contains 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
/* POINT */ | |
var Point = { | |
init: function(x, y) { | |
this.x = x; | |
this.y = y; | |
}, | |
move: function(deltaX, deltaY) { | |
this.x += deltaX; | |
this.y += deltaY; |
This file contains 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
/* POINT */ | |
function Point(x, y) { | |
this.x = x; | |
this.y = y; | |
} | |
Point.prototype.move = function(deltaX, deltaY) { | |
this.x += deltaX; | |
this.y += deltaY; |
This file contains 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
/* | |
Поиск количества разворотов массива за O(n) | |
*/ | |
function countRotations(arr) { | |
var min = arr[0], minIndex = 0; | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] < min) { | |
min = arr[i]; | |
minIndex = i; | |
} |
This file contains 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
if (!Object.create) { | |
Object.create = function(o) { | |
function F() {} | |
F.prototype = o; | |
return new F(); | |
}; | |
} |
This file contains 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
var order = ['A', 'B', 'C']; | |
var memory = []; | |
function worker() { | |
// Находим в памяти индекс нужного нам по порядку значения | |
var i = memory.findIndex(function(curr) { | |
return order[0] === curr; | |
}); | |
// Если оно есть |
This file contains 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
function comp(firstItem, secondItem) { | |
const firstCoeff = firstItem['value'] / firstItem['weight']; | |
const secondCoeff = secondItem['value'] / secondItem['weight']; | |
return secondCoeff - firstCoeff; | |
} | |
function knapsack(w, items) { | |
let result = []; | |
items = items.sort(comp); |
This file contains 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
function comp(a, b) { | |
return b - a; | |
} | |
function atm(total, values) { | |
values = values.sort(comp); | |
let counts = {}; | |
let success = true; |
This file contains 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
function isBalanced(expr) { | |
let stack = []; | |
for (let i = 0, length = expr.length; i < length; i++) { | |
if (expr[i] === '{') { | |
stack.push(expr[i]); | |
} else if (expr[i] === '}') { | |
if (stack.length > 0) { | |
stack.pop(); | |
} else { |
This file contains 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
function isPalindrome(str) { | |
str = str.toLowerCase().replace(/\s/g, ''); | |
return str === str.split('').reverse().join(''); | |
} | |
function isPalindromeWithoutReverse(str) { | |
str = str.toLowerCase().replace(/\s/g, ''); | |
let result = true; | |
let l = 0; |
NewerOlder