Skip to content

Instantly share code, notes, and snippets.

View dolvik's full-sized avatar

Viktor Kireev dolvik

View GitHub Profile
@dolvik
dolvik / index.js
Last active October 11, 2017 12:15
Log function that is used ing https://github.com/visionmedia/debug
//https://github.com/visionmedia/debug
function log() {
// this hackery is required for IE8/9, where
// the `console.log` function doesn't have 'apply'
return 'object' === typeof console
&& console.log
&& Function.prototype.apply.call(console.log, console, arguments);
}
@dolvik
dolvik / index.js
Created April 12, 2017 11:59
Small util functions
const delayed = (ms, fn, ctx) => (...args) => {
setTimeout(fn.bind(ctx, ...args), ms);
};
const revert = (arr) => arr.reduce((acc, curr, i, arr) => acc.concat(arr[arr.length - i - 1]), [] );
# Your init script
#
# Atom will evaluate this file each time a new window is opened. It is run
# after packages are loaded/activated and after the previous editor state
# has been restored.
#
# An example hack to log to the console when each text editor is saved.
#
# atom.workspace.observeTextEditors (editor) ->
# editor.onDidSave ->
@dolvik
dolvik / index.js
Created September 2, 2016 07:24
Преобразование дерева в пункты меню по уровням вложенности
/*
Результат
[
['1', '2', '3'],
['1-1', '1-2', '3-1', '3-2', '3-3'],
['1-2-1', '3-2-1', '3-2-2']
]
*/
var input = [
@dolvik
dolvik / index.js
Created September 2, 2016 06:54
JS functional calc
var five = num(5);
var one = num(1);
console.log(five(plus(one()))); //6
function plus(arg){
return function(arg2){
return arg + arg2;
}
}
@dolvik
dolvik / index.js
Created May 31, 2016 09:16
Triangle. Determine whether a triangle can be built from a given set of edges.
//http://codility.com/demo/take-sample-test/triangle
function solution(arr) {
if (arr.length < 3){
return 0;
}
arr.sort(function(a, b){return a - b;});
for (var i = 0, len = arr.length; i < len - 2; i++){
@dolvik
dolvik / index.js
Created May 31, 2016 08:27
Properly nested string
/*
A string S consisting of N characters is considered to be properly nested if any of the following conditions is true:
S is empty;
S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
S has the form "VW" where V and W are properly nested strings.
For example, the string "{[()()]}" is properly nested but "([)()]" is not.
*/
var rb = {
@dolvik
dolvik / index.js
Created May 30, 2016 11:23
Баннерокрутилка
//взять случайное число из пространства длины массива
//переставить элемент с этим индексом и последний элемент
//уменьшить индекс длины на единицу
//повторить все это в цикле пока не дойдем до первого элемента
function getNewArray(arr){
var res = arr.slice();
var max = res.length - 1;
var i, e;
while(max > 0){
i = getRandomIntInclusive(0, max);
@dolvik
dolvik / index.js
Last active May 30, 2016 09:50
Palindrome
//Способ с перебором массива
function isPalindrome(s){
s = s.toLowerCase().replace(/[^а-яё]/g, "");
for (var i = 0; i < s.length / 2; i++){
var b = s[i];
var e = s[s.length - 1 - i];
if (b !== e)
{
return false;
@dolvik
dolvik / index.js
Last active April 4, 2017 10:27
Inheritence in Javascript
//Раньше наследование делали с помощью такой функции-помощника.
//Вся цель этой функции - присвоить свойству prototype дочернего класса новый объект, прототип которого будет ссылаться на свойство prototype объекта parent
function inherit(child, parent){
//создается вспомагательная функция, т.е. объект типа Function
function F(){};
//объект типа Function имеет свойство prototype, которое мы перезаписываем на parent.prototype
F.prototype = parent.prototype;
//создаем новый объект, прототипом которого будет F.prototype, который в свою очередь ссылается на parent.prototype
//т.е. new F() создает объект obj:
//obj.__proto__ = parent.prototype