$('.news-block-2').each((i, n) => $('.text-news', n).after($('.picture-news', n)));
$('.news-block-2 tr').append(function() {
return $('.picture-news', this);
});
$('.news-block-2 .text-news').each((i, n) => $(n).parent().prepend(n));
$('.news-block-2 .picture-news').before(function() {
return $(this).next();
});
X = n => n ? (n & 1 ? '-chirp' : '') + X(n >> 1) + X(n >> 1) : ''
chirp = n => X(n).slice(1)
google.visualization.arrayToDataTable(data, true)
Array
.from(document.querySelectorAll('.obj'))
.filter(n => n.querySelector('.who')?.innerText !== 'Я')
.forEach(n => console.log(n.innerText));
const $items = $('.item');
const SLICE_SIZE = 3;
$('.container').append(i => $items.slice(i * SLICE_SIZE, (i + 1) * SLICE_SIZE));
$('label > input').change(function() {
$(this).parent().toggleClass('active', this.checked);
});
const containerSelector = '.card_options > div';
const itemSelector = '.option';
const activeClass = 'checked';
const toggleItem = item => item
?.closest(containerSelector)
.querySelectorAll(itemSelector)
.forEach(n => n.classList[n === item ? 'toggle' : 'remove'](activeClass));
// можно добавить обработчик клика каждому элементу индивидуально
document.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => toggleItem(e.currentTarget));
// а можно один раз - делегированный, всей странице
document.addEventListener('click', e => {
toggleItem(e.target.closest(itemSelector));
});
const isArithmeticProgression = arr =>
arr.length > 1
? new Set(arr.map((n, i, a) => n - a[i + 1]).slice(0, -1)).size === 1
: !!arr.length;
// или
const isArithmeticProgression = arr =>
!!arr.length && arr.every((n, i, a) => !i || (n - a[i - 1] === a[1] - a[0]));
const arr = Object.values(obj);
. Дальше есть варианты:const result = arr.sort((a, b) => a.position - b.position).map(n => n.color);
const result = arr
.reduce((acc, n) => ((acc[n.position] = acc[n.position] ?? []).push(n.color), acc), [])
.flat();
const result = Object.values(Object.fromEntries(arr.map(n => [ n.position, n.color ])));
document.querySelectorAll('.row').forEach(addClass);
// или
for (const n of document.getElementsByClassName('row')) {
addClass(n);
}
const addClass = el => el.classList.add(`row-${el.children.length}`);
const classes = {
3: 'row-3',
5: 'row-5',
7: 'row-xxx',
};
function addClass(el) {
const className = classes[el.children.length];
if (className) {
el.classList.add(className);
}
}
const classes = [
{ max: 3, name: 'row-0-3' },
{ max: 5, name: '' }, // если дочерних элементов 4 или 5, класс не добавляется
{ max: 7, name: 'row-6-7' },
{ max: Infinity, name: 'row-7-x' },
];
function addClass(el) {
const { name } = classes.find(n => n.max >= el.children.length) ?? {};
if (name) {
el.classList.add(name);
}
}
const classes = [
[ c => c < 5, 'row-0-4' ],
[ c => c % 2, 'row-odd' ],
[ c => 6 < c && c < 9, 'row-7-8' ],
];
const addClass = ({ classList, children: { length } }) =>
classList.add(...classes.reduce((acc, n) => (
n[0](length) && acc.push(n[1]),
acc
), []));
$('.entry__arrow--increment, .entry__arrow--decrement').click(function() {
const $this = $(this);
$this.closest('.entry__box').find('.entry__data').text(function(i, text) {
const { step, max, min } = this.dataset;
let newVal = +text + step * ($this.hasClass('entry__arrow--increment') ? 1 : -1);
if (newVal > max) {
newVal = min;
} else if (newVal < min) {
newVal = max;
}
return `${newVal}`.padStart(2, 0);
});
});
const li = document.querySelectorAll('li');
li.forEach(n => n.addEventListener('click', onClick));
function onClick(e) {
e.preventDefault();
li.forEach(n => n.classList.toggle('active', n === this));
const href = this.querySelector('a').getAttribute('href');
document.querySelector(href).scrollIntoView({
behavior: 'smooth',
});
}
const parent = document.querySelector('.team');
const wrapperTag = 'div';
const wrapperClass = 'new';
const wrapSize = 2;
parent.querySelectorAll(':scope > *').forEach((n, i) => {
if (!(i % wrapSize)) {
parent.appendChild(document.createElement(wrapperTag));
parent.lastChild.classList.add(wrapperClass);
}
parent.lastChild.appendChild(n);
});
parent.append(...Array.from(
{ length: Math.ceil(parent.children.length / wrapSize) },
() => {
const wrapper = document.createElement(wrapperTag);
wrapper.className = wrapperClass;
wrapper.append(...Array.prototype.slice.call(parent.children, 0, wrapSize));
return wrapper;
}
));