if (i == day && month == currentMonth) { calendarView += "<td class='currentday'>" + i + "</td>";
document.body.innerHTML = `
<table class="calendar">
<thead>
<tr>
<th data-step="-1"><</th>
<th class="month" colspan="5"></th>
<th data-step="+1">></th>
</tr>
<tr class="weekdays">${Array.from({ length: 7 }, (_, i) => `
<th>${
new Date(2001, 0, i).toLocaleString('en', { weekday: 'short' })}
</th>`).join('')}
</tr>
</thead>
<tbody></tbody>
</table>
`;
const calendar = document.querySelector('table');
const month = calendar.querySelector('.month');
const tbody = calendar.tBodies[0];
const date = new Date;
calendar.querySelectorAll('[data-step]').forEach(n => {
n.addEventListener('click', showNextMonth);
});
function showNextMonth({ target: { dataset: { step } } }) {
date.setMonth(date.getMonth() + +step);
renderCalendar();
}
function renderCalendar() {
tbody.innerHTML = '';
month.innerText = date.toLocaleString('en', {
year: 'numeric',
month: 'long',
});
const m = date.getMonth();
const d = new Date(date.getFullYear(), m);
const today = new Date().setHours(0, 0, 0, 0);
do {
d.setDate(d.getDate() - 1);
} while (d.getDay() !== 0);
for (let i = 0; i < 6; i++) {
const tr = tbody.insertRow();
for (let j = 0; j < 7; j++, d.setDate(d.getDate() + 1)) {
const td = tr.insertCell();
td.innerHTML = d.getDate();
td.classList.toggle('today', +d === today);
td.classList.toggle('current-month', m === d.getMonth());
}
}
}
renderCalendar();
const form = document.querySelector('form');
document.querySelector('button').addEventListener('click', () => {
form.querySelectorAll('fieldset').forEach(n => n.disabled = false);
// или
for (const n of form.getElementsByTagName('fieldset')) {
n.removeAttribute('disabled');
}
// или
for (let i = 0; i < form.elements.length; i++) {
if (form.elements[i].tagName === 'FIELDSET') {
form.elements[i].attributes.removeNamedItem('disabled');
}
}
});
const div = document.createElement('div');
div.innerHTML = str;
const arr = Array.prototype.map.call(div.children, n => n.outerHTML);
const arr = Array.from(
new DOMParser().parseFromString(str, 'text/html').body.children,
n => n.outerHTML
);
const arr = [];
for (const n of document.createRange().createContextualFragment(str).children) {
arr.push(n.outerHTML);
}
function countBinaryDigits(number) {
const count = [ 0, 0 ];
do {
count[number & 1]++;
} while (number >>= 1);
return count;
}
// или
const countBinaryDigits = number => Array
.from(number.toString(2))
.reduce((acc, n) => (acc[n]++, acc), [ 0, 0 ]);
const numbers = [ 0, 1, 15, 0b1010010111010 ];
console.table(numbers.map(function(num) {
return [ num ]
.concat(countBinaryDigits(num))
.reduce((acc, n, i) => (acc[this[i]] = n, acc), {});
}, [ 'число', 'нули', 'единицы' ]));
var lastCharacter = ';';
$('input').on('input', function() {
var val = this.value;
if (val && val[val.length - 1] !== lastCharacter) {
$(this).val(val + lastCharacter);
}
});
я что-то не так делаю видно
<div class="container">
<input maxlength="10">
</div>
const container = document.querySelector('.container');
const input = container.querySelector('input').cloneNode();
container.addEventListener('input', ({ target: t }) => {
if (t.value.length === +t.maxLength) {
const nextInput = t.nextElementSibling || input.cloneNode();
t.insertAdjacentElement('afterend', nextInput);
nextInput.focus();
}
});
const a = v1 => {
const f = v2 => a(v1 + v2);
f.valueOf = () => v1;
return f;
};
a(1)(2)(3)(4)(5) * 6 // 90
10 + a(4)(5)(6) // 25
Math.pow(a(8)(1), 3) // 729
a(4)(6) - a(3) // 7
Переписал код с ES5 на ES6
const adjacentElementsProduct = arr => arr
.sort((a, b) => a - b)
.slice(-2)
.reduce((acc, n) => acc * n, 1);
arr = arr.filter(n => n.id !== ID);
arr.splice(arr.findIndex(n => n.id === ID), 1);
const index = arr.findIndex(n => n.id === ID);
if (index !== -1) {
arr.splice(index, 1);
}
function toggleClass(selector, className, delay) {
let index = -1;
return setInterval($items => {
$items.eq(index).removeClass(className);
index = (index + 1) % $items.length;
$items.eq(index).addClass(className);
}, delay, $(selector));
}
const intervalId = toggleClass('.wrap div', 'active', 300);
function toggleClass(selector, className, delay) {
const items = document.querySelectorAll(selector);
let index = ~-items.length;
return items.length
? setInterval(() => {
items[index].classList.remove(className);
index = -~index % items.length;
items[index].classList.add(className);
}, delay)
: null;
}
document.querySelectorAll('.item-parent').forEach(n => {
n.insertAdjacentHTML('beforeend', '<div class="item-decor"></div>');
});
for (const n of document.getElementsByClassName('item-parent')) {
const div = document.createElement('div');
div.classList.add('item-decor');
n.appendChild(div);
}
function prev(el, selector) {
if (el instanceof HTMLElement) {
el = el.previousElementSibling;
return el && (!selector || el.matches(selector)) ? el : null;
}
if (el && Number.isInteger(el.length) && el.length >= 0) {
return Array.prototype.reduce.call(
el,
(acc, n) => ((n = prev(n, selector)) && acc.push(n), acc),
[]
);
}
return null;
}
// можно передавать как одиночный элемент (результатом будет тоже элемент или null)...
prev(document.body)
// ... так и коллекции (результатом будет массив)
prev(document.images)
prev(document.querySelectorAll('div'), 'span')
const $sorted = $('table tr').sort(el => $(el).hasClass('important') ? -1 : 1);
$sorted.appendTo($sorted.parent());
const $important = $('table tr.important');
$important.parent().prepend($important);