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
"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++) { |
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
//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; }); | |
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
"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) { |
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 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) { |
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
<!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()"> |
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
//Добавить событие longtap, которое возникает при длительном (1с) нажатии на элемент | |
//Переопределяется метод addEventListener у прототипа Element, таким образом, что: | |
//Если новый метод вызывается для события touchstart, то callback метода декорируется: | |
//- у элемента проставляется флаг isTouched | |
//- перед вызовом исходного callback запускается timeout, по окончании которого проверится флаг isTouched и вызовется запуск события longtap | |
//Если новый метод вызывается для события touchend, то callback метода декорируется: | |
//- снимается флаг isTouched | |
const longtapEvent = new Event("longtap"); |
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
<div class="container"> | |
<div class="inner"> | |
<div class="text"> | |
Lorem ipsum dolor sit amet. | |
</div> | |
</div> | |
</div> |
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
//Раньше наследование делали с помощью такой функции-помощника. | |
//Вся цель этой функции - присвоить свойству 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 |
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(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; |
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 getNewArray(arr){ | |
var res = arr.slice(); | |
var max = res.length - 1; | |
var i, e; | |
while(max > 0){ | |
i = getRandomIntInclusive(0, max); |
OlderNewer