str.replace(/.{4}(?!$)/g, '$&-')
// или
str.match(/.{1,4}/g).join('-')
// или
str.split(/(?<=^(?:.{4})+)/).reduce((acc, n) => acc + (acc && '-') + n, '')
// или
''.concat(...[...str].map((n, i) => !i || i % 4 ? n : `-${n}`))
// или
Array.from(
{ length: Math.ceil(str.length / 4) },
(n, i) => str.slice(i * 4, (i + 1) * 4)
).join`-`
const items = document.querySelectorAll('.yourself-item__title');
const height = `${Math.max(...Array.from(items, n => n.clientHeight))}px`;
items.forEach(n => n.style.height = height);
.accordion__grandchild__item
, а переключаете видимость у родительских элементов - .accordion__grandchild__group
. Надо как-то устранить это печальное несоответствие. $('select').change(function() {
const v = this.value;
$('.sum').text($(`.element${v === '*' ? '' : `.${v}`}`).length);
});
document.querySelector('select').addEventListener('change', e => {
const v = e.target.value;
const s = '.element' + (v === '*' ? '' : '.' + v);
document.querySelector('.sum').textContent = document.querySelectorAll(s).length;
});
<select data-prop="type">
<option hidden></option>
<option value="type1">hello, world!!</option>
<option value="type2">fuck the world</option>
<option value="type3">fuck everything</option>
</select>
<br>
<input placeholder="name1" data-prop="name">
<input placeholder="name2" data-prop="name">
<input placeholder="name3" data-prop="name">
<br>
<input placeholder="url1" data-prop="url">
<input placeholder="url2" data-prop="url">
<input placeholder="url3" data-prop="url">
<br>
<input class="value" disabled>
<input class="value" disabled>
<input class="value" disabled>
const getValues = prop =>
Array.from(document.querySelectorAll(`[data-prop="${prop}"]`), n => n.value);
document.addEventListener('input', e => {
if (!e.target.dataset.prop) {
return;
}
const type = getValues('type')[0];
const names = getValues('name');
const urls = getValues('url');
document.querySelectorAll('.value').forEach((n, i) => {
n.name = n.value = `xxx[${type}][${names[i]}][${urls[i]}]`;
});
});
if (item.innerText === currentPage) {
item.innerText == currentPage
+item.innerText === currentPage
$('.input_1').each((i, n) => console.log(n, n.value ? 'я заполнен' : 'я пуст'));
console.log($('.input_1').get().some(n => n.value) ? 'кто-то заполнен' : 'все пустые');
console.log($('.input_1').toArray().every(n => n.value) ? 'все заполнены' : 'кто-то пуст');
console.log(Array.from($('.input_1')).filter(n => n.value), 'мы заполнены');
console.log([...$('.input_1').not((i, n) => n.value)], 'мы пустые');
function getDateInTimeZone(utcOffset, date = new Date()) {
const utcTime = date.getTime() + date.getTimezoneOffset() * 60000;
return new Date(utcTime + utcOffset * 3600000);
}
const moscowDate = getDateInTimeZone(3);
const newYorkDate = getDateInTimeZone(-5);
const tokyoDate = getDateInTimeZone(9);
const digital_root = num => num > 9
? digital_root([...`${num}`].reduce((acc, n) => acc + +n, 0))
: num;
const getNested = (obj, keys) => keys.reduce((p, c) => p?.hasOwnProperty(c) ? p[c] : null, obj);
Object.keys(obj).forEach(n => n !== key && delete obj[n]);
obj = { [key]: obj[key] };
const deleteKeys = (obj, except) =>
Object.keys(obj).forEach(n => except.includes(n) || delete obj[n]);
const obj = { a: 1, b: 2, c: 3, d: 4 };
deleteKeys(obj, [ 'a' ]);
console.log(obj); // {a: 1}
const pick = (obj, keys) =>
Object.fromEntries(keys.map(n => [ n, obj[n] ]));
const obj = { a: 1, b: 2, c: 3, d: 4 };
console.log(pick(obj, [ 'a', 'd' ])); // {a: 1, d: 4}
const className = 'select';
const sumEl = document.querySelector('.sum');
const sum = elements =>
Array.prototype.reduce.call(
elements,
(acc, n) => acc + (+n.value || 0),
0
);
const updateSum = () => sumEl.textContent = sum(document.getElementsByClassName(className));
document.addEventListener('change', e => e.target.classList.contains(className) && updateSum());
updateSum();
const selects = document.querySelectorAll(`.${className}`);
const updateSum = () => sumEl.innerText = sum(selects);
selects.forEach(n => n.addEventListener('change', updateSum));
updateSum();
.submenu
, он должен находится внутри того же li
, что и соответствующий ему .profile-menu-trigger
.$('.profile-menu')
.on('mouseenter', '.profile-menu-trigger', function() {
$(this).next('.submenu').fadeIn(500);
})
.on('mouseleave', 'li', function() {
$(this).find('.submenu').fadeOut(500);
});
console.log(Object.entries(obj).reduce((max, n) => n[1] > max[1] ? n : max).join(': '))
if (counting >= 10) { level++; counting = 0;
function getScore(arr) {
const points = [ 0, 40, 100, 300, 1200 ];
let score = 0;
let lines = 0;
for (const n of arr) {
score += points[n] * (1 + (lines / 10 | 0));
lines += n;
}
return score;
}