Skip to content

Instantly share code, notes, and snippets.

View dolvik's full-sized avatar

Viktor Kireev dolvik

View GitHub Profile
@dolvik
dolvik / index.html
Last active May 26, 2016 14:34
Position element vertically centered in the outer element with known height
<div class="container">
<div class="inner">
<div class="text">
Lorem ipsum dolor sit amet.
</div>
</div>
</div>
@dolvik
dolvik / index.js
Created May 26, 2016 14:14
Long tap event
//Добавить событие longtap, которое возникает при длительном (1с) нажатии на элемент
//Переопределяется метод addEventListener у прототипа Element, таким образом, что:
//Если новый метод вызывается для события touchstart, то callback метода декорируется:
//- у элемента проставляется флаг isTouched
//- перед вызовом исходного callback запускается timeout, по окончании которого проверится флаг isTouched и вызовется запуск события longtap
//Если новый метод вызывается для события touchend, то callback метода декорируется:
//- снимается флаг isTouched
const longtapEvent = new Event("longtap");
@dolvik
dolvik / index.html
Created May 26, 2016 14:08
Limit number of function running at the same time
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Call any function just once within a period of time">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<input type="button" value="Call function 3 times" onclick="onClick()">
@dolvik
dolvik / index.js
Last active May 26, 2016 14:15
Find combinations of numbers in an array with determined sum
var ARR_MIN_LENGTH = 5;
var ARR_MAX_LENGTH = 10;
var arr = getArbitraryArray();
//var arr = [1, 10, 3, 2, 7, 3, 8, 9];
console.log(arr, find(arr, 10));
//arr - массив натуральных чисел.
function find(arr, target) {
@dolvik
dolvik / index.js
Last active May 26, 2016 14:24
Sequences
"use strict";
var m = 3;
var n = 4;
function next(seq) {
//find i, seq[i] < m, seq[i + 1] = seq[n] = m
var i = n - 1;
console.log(seq);
while (i >= 0 && seq[i] == m) {
@dolvik
dolvik / index.js
Last active May 31, 2016 07:37
Binary gap
//Find longest sequence of zeros in binary representation of an integer.
function solution(n) {
var str = toBinaryString(n).replace(/0+$/, "");
var arr = str.split("1").filter(function(e) { return e.length > 0; });
if (arr.length === 0){
return 0;
}
arr.sort(function(a, b){ return b.length - a.length; });
@dolvik
dolvik / index.js
Last active May 26, 2016 14:23
Equilibrium index
"use strict";
//An equilibrium index of a sequence is an index into the sequence such that the sum of elements at lower indices is equal to the sum of elements at higher indices.
//two loops
function eq(arr) {
if (arr.length === 0) {
return -1;
}
for (var i = 0; i < arr.length; i++) {