const $table = $('table');
const columnIndex = // здесь должен быть номер столбца с числами
const sum = $table
.find('tr:visible')
.get()
.reduce((acc, n) => acc + +$('td', n).eq(columnIndex).text(), 0);
$table
.find('th')
.eq(columnIndex)
.text(`Вес (${sum})`);
$('.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
), []));