document.querySelector('button').addEventListener('click', function() {
const val = +document.querySelector('#elem1').value;
const sign = Math.sign(val);
document.querySelector('#str').innerHTML = Array
.from({ length: val * sign + 1 }, (n, i) => val - i * sign)
.join('<br>');
});
$('.nextArrow, .backArrow').on('animationend', e => e.target.style.animation = '');
function openCity(e, city) {
document.querySelectorAll('.tablink').forEach(n => {
n.classList.toggle('active', city === 'All' || n === e.target);
});
document.querySelectorAll('.tabcontent').forEach(n => {
n.style.display = city === 'All' || n.id === city ? 'block' : 'none';
});
}
document.querySelectorAll('.number').forEach(number => {
const top = number.getBoundingClientRect().top;
window.addEventListener('scroll', function onScroll() {
if (window.pageYOffset > top - window.innerHeight / 2) {
this.removeEventListener('scroll', onScroll);
let start = +number.innerHTML;
const interval = setInterval(function() {
number.innerHTML = ++start;
if (start >= number.dataset.max) {
clearInterval(interval);
}
}, 5);
}
});
});
$(document).wheel(
var item = $('#time'); if (e.deltaY > 0) item.scrollLeft += 100;
$(document).on('wheel', function(e) {
$('#time')[0].scrollLeft += e.originalEvent.deltaY > 0 ? 100 : -100;
// или
$('#time').get(0).scrollLeft += [ -100, 100 ][+(e.originalEvent.deltaY > 0)];
// или
$('#time').prop('scrollLeft', (i, val) => val + 100 * Math.sign(e.originalEvent.deltaY));
});
typeof
:const strings = arr.filter(n => typeof n === 'string');
const numbers = arr.filter(n => typeof n === 'number');
const booleans = arr.filter(n => typeof n === 'boolean');
const strings = arr.filter(n => n === `${n}`);
const numbers = arr.filter(n => n === +n); // в отличие от typeof, отбрасывает NaN
const booleans = arr.filter(n => n === !!n);
const groupedByType = arr.reduce((acc, n) => {
const type = n == null ? `${n}` : n.constructor.name.toLowerCase();
(acc[type] = acc[type] || []).push(n);
return acc;
}, {});
const strings = groupedByType.string;
const containerSelector = 'ul';
const itemSelector = 'li';
const activeClass = 'active';
const $containers = $(containerSelector).on('click', itemSelector, function(e) {
const index = $(itemSelector, e.delegateTarget).index(this);
$containers.find(`${itemSelector}.${activeClass}`).removeClass(activeClass);
$containers.find(`${itemSelector}:eq(${index})`).addClass(activeClass);
});
const containers = document.querySelectorAll(containerSelector);
containers.forEach(n => n.addEventListener('click', onClick));
function onClick({ target: t }) {
if (t = t.closest(itemSelector)) {
const items = this.querySelectorAll(itemSelector);
const index = Array.prototype.indexOf.call(items, t);
containers.forEach(container => {
container.querySelectorAll(itemSelector).forEach((n, i) => {
n.classList.toggle(activeClass, i === index);
});
});
}
}
const td = [...document.querySelectorAll('table td')];
const first = td[0].offsetLeft;
const last = Math.max(...td.map(n => n.offsetLeft + n.offsetWidth));
td.forEach(n => (
(n.offsetLeft === first) && n.classList.add('first'),
(n.offsetLeft + n.offsetWidth === last) && n.classList.add('last')
));
const newArr = arr.reduce((acc, n) => (
acc[n.month - 1] = n.sum,
acc
), Array(12).fill(''));
const newArr = Array.from({ length: 12 }, function(_, i) {
return this[-~i] || '';
}, Object.fromEntries(arr.map(n => [ n.month, n.sum ])));
const newArr = [];
for (let i = 0, j = 0; i < 12; i++) {
newArr.push((arr[j] || {}).month === i + 1 ? arr[j++].sum : '');
}
!==
) и закрыт, т.е., открываете все, кроме закрываемого.document.addEventListener('click', e => {
const t = e.target;
const heading = t.closest('.panel-heading');
const nextStep = t.closest('.construct-btn');
const collapse =
heading ? heading.nextElementSibling :
nextStep ? t.closest('.panel').nextElementSibling.querySelector('.panel-collapse') :
null;
if (collapse) {
e.preventDefault();
t.closest('.panel-group').querySelectorAll('.panel-collapse').forEach(n => {
n.classList[n === collapse ? 'toggle' : 'remove']('in');
});
}
});
function addClickListeners(buttonsSelector, dialogSelector) {
const buttons = document.querySelectorAll(buttonsSelector);
const dialog = document.querySelector(dialogSelector);
buttons.forEach(n => n.addEventListener('click', e => {
e.preventDefault();
dialog.style.display = 'block';
}));
dialog.addEventListener('click', ({ target }) => {
if (target.classList.contains('popup-close')) {
document.getElementById('name_1').disabled = true;
document.getElementById('phone_1').disabled = true;
dialog.style.display = 'none';
} else if (!target.closest('.popup-content')) {
dialog.style.display = 'none';
}
});
}
addClickListeners('header .contacts a', '.popup-call');
addClickListeners('.sentence-btn', '.popup-discount');
addClickListeners('.check-btn', '.popup-check');
document.addEventListener('click', function(e) {
const heading = e.target.closest('.panel-heading');
if (heading) {
e.preventDefault();
heading.closest('.panel-group').querySelectorAll('.panel-collapse').forEach(n => {
n.classList[n === heading.nextElementSibling ? 'toggle' : 'remove']('in');
});
}
});
const result = array1.filter(n => !array2.some(m => m.name === n.name));
function diff(data1, data2, key = n => n) {
const result = [];
const getKey = key instanceof Function ? key : n => n[key];
const keys = new Set(Array.from(data2, getKey));
for (const n of data1) {
if (!keys.has(getKey(n))) {
result.push(n);
}
}
return result;
}
// ваш случай
const result = diff(array1, array2, 'name');
// есть и другие варианты применения
diff([ 1, 2, 3, 4, 5 ], [ 1, 2, 3 ]) // [4, 5]
diff(Array(10).keys(), Array(7).keys()) // [7, 8, 9]
diff('abcde', 'ACE', n => n.toLowerCase()) // ['b', 'd']