const add = (str, val) =>
str.replace(/\d+$/, m => `${+m + val}`.padStart(m.length, 0));
add('string0001', 1) // 'string0002'
add('string1010', 99) // 'string1109'
add('string2345', 6789) // 'string9134'
string99 + 1
должно быть равно string00
, а не string100
), то после вызова padStart
добавьте .slice(-m.length)
. <p class="typeit">hello, world!!</p>
<p class="typeit">fuck the world</p>
<p class="typeit">fuck everything</p>
const typeit = Array.from(
document.querySelectorAll('.typeit'),
(n, i) => new TypeIt(n, {
cursor: false,
afterComplete: () => typeit[i + 1]?.go(),
})
);
typeit[0].go();
const uniqueWithCount = (arr, idKey, countKey) =>
Object.values(arr.reduce((acc, n) => (
(acc[n[idKey]] ??= { ...n, [countKey]: 0 })[countKey]++,
acc
), {}));
const result = uniqueWithCount(arr, 'name', 'qty');
const addToCart = product =>
setCart(cart => cart.some(n => n.id === product.id)
? cart.map(n => n.id === product.id ? { ...n, qty: n.qty + 1 } : n)
: [ ...cart, { ...product, qty: 1 } ]
);
const firstNonRepeatingLetter = str =>
[...str].find((n, i, a) => a.indexOf(n) === a.lastIndexOf(n)) || '';
const firstNonRepeatingLetter = str =>
str.charAt(Array
.from(str.toLowerCase())
.findIndex((n, i, a) => a.indexOf(n) === a.lastIndexOf(n))
);
separator = 'lot'
item = 'obj'
count = [ n.count(item) for n in s.split(separator)[1:] ]
print(' '.join(f'[{separator} {n} {item}]' for n in count))
function calc(val = 0) {
const self = {
add: v => (val += v, self),
sub: v => (val -= v, self),
mul: v => (val *= v, self),
div: v => (val /= v, self),
pow: v => (val **= v, self),
toString: () => val,
};
return self;
}
calc().add(5).mul(5) + 1 // 26
+calc(100).div(10).sub(2) // 8
`${calc(2).pow(10)}` // "1024"
const sum = (data, getVal) => Object
.values(data instanceof Object ? data : {})
.reduce((acc, n) => acc + sum(n, getVal), getVal(data));
sum([ 1, 2, 3, '1', [ '2', 4, [ '3' ] ], '4' ], x => +(typeof x === 'string')) // 4
sum({ a: { b: [ NaN, Infinity ], c: 123456 } }, x => +(typeof x === 'number')) // 3
function sum(a) {
const f = b => sum(a + b);
f.valueOf = () => a;
return f;
}
sum(1)(2)(3) + 4 // 10
sum(5) * sum(6) // 30
700 / sum(7) // 100
Math.pow(sum(8), 2) // 64
// но если попутно не выполняется никаких числовых операций, придётся немного поработать руками:
+sum(9)(10) // 19
Number(sum(11)(12)) // 23
const dropdown = document.querySelector('.dropdown-el');
const activeClass = 'expanded';
dropdown.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();
e.currentTarget.classList.toggle(activeClass);
e.target.previousElementSibling.checked = true;
});
document.addEventListener('click', () => dropdown.classList.remove(activeClass));
priceData.map(n => ({
...n,
services: n.services.filter(m => m.name.toLowerCase().includes('вакцина')),
}));
data: () => ({
opened: [],
...
}),
watch: {
opened: {
immediate: true,
handler(val) {
const id = this.treeitems[0].id;
if (!val.includes(id)) {
val.push(id);
}
}
},
...
},
:open.sync="opened"
Разбираюсь, как устроен vuejs
в this экземпляр vue