const result = arr.flat().map((n, i) => ({ ...n, id: -~i }));
const result = [];
for (const n of arr) {
for (const m of n) {
result.push({
...m,
id: result.length + 1,
});
}
}
context.translate(120, 120);
сделайте context.translate(120 + x_pos, 120);
, а context.fillRect(x_pos, 0, 100, 100);
замените на context.fillRect(0, 0, 100, 100);
. const coord = [ 0, 0 ];
const step = 10;
const moveFunc = e => {
const shift = ({
ArrowUp: [ 0, -1 ],
ArrowDown: [ 0, 1 ],
ArrowLeft: [ -1, 0 ],
ArrowRight: [ 1, 0 ],
})[e.code];
if (shift) {
div.style.left = `${coord[0] += shift[0] * step}px`;
div.style.top = `${coord[1] += shift[1] * step}px`;
}
};
const result = Array.from(
document.querySelectorAll('#spisok > div > p > br'),
n => n.nextSibling.textContent.trim()
);
const result = Array.prototype.map.call(
document.getElementById('spisok').getElementsByTagName('button'),
n => n.previousSibling.nodeValue.replace(/(^\s+)|(\s+$)/g, '')
);
const makeOrderList = str =>
Object.fromEntries(Array.from(
str.matchAll(/(\d+) ([^,]+)/g),
n => [ n[2].replace(/ /g, '_'), +n[1] ]
));
const makeOrderList = str => str
.split(', ')
.map(n => [ n.split(' ').slice(1).join('_'), parseInt(n) ])
.filter(n => !Number.isNaN(n[1]))
.reduce((acc, n) => (acc[n[0]] = n[1], acc), {});
const attrName = 'bst-click';
const elements = document.querySelectorAll(`[${attrName}]`);
const data = Array.from(
elements,
n => [
n.attributes[attrName].value,
n.classList.value,
]
);
const data = Array.prototype.reduce.call(
elements,
(acc, n) => (
(acc[n.getAttribute(attrName)] ??= []).push(n.className),
acc
),
{}
);
нужно в 24 строки уложиться
return ''.join('0' if n == '1' else '1' for n in d)
return '1' if n == 1 else ((s := dracon(n - 1)) + '1' + invert(s)[::-1])
dr
, идентичны одной из веток условного оператора внутри цикла - так что удалим их, а цикл пусть сделает ещё одну итерацию, для этого в начале dr
должно оказаться что-то, отличное от '1'
.invert = lambda d: ''.join('0' if n == '1' else '1' for n in d)
dracon = lambda n: '1' if n == 1 else ((s := dracon(n - 1)) + '1' + invert(s)[::-1])
dr = '0' + dracon(int(input()))
print(dr)
import turtle as t
tt = t.Turtle()
for n in dr:
if n == '1':
tt.right(90)
tt.forward(4)
else:
tt.left(90)
tt.forward(4)
input()
:=
).const Indicator = ({ value, max = 6 }) => (
<div>
{Array.from({ length: max }, (_, i) => (
<div className={`indicator-cell ${i < value ? 'active' : ''}`}></div>
))}
</div>
);
.indicator-cell {
background: white;
}
.indicator-cell.active {
background: red;
}
<Indicator value={4} />
<Indicator value={1} />
<Indicator value={8} max={12} />
computed: {
highlightedText() {
const { text, search } = this;
return search
? text.split(RegExp(`(${search.replace(/[\\^$|.*?+{}()[\]]/g, '\\$&')})`, 'gi'))
: [ text ];
},
},
<template v-for="(n, i) in highlightedText">
<mark v-if="i % 2">{{ n }}</mark>
<template v-else>{{ n }}</template>
</template>
addEventListener
в качестве третьего аргумента { once: true }
. $('#add-instr').click(function() {
$('#column-left').append(`
<article class="instruction">
<div class="name">
${$('#i-name').val()}
<button class="remove">x</button>
</div>
<div class="desc">
${$('#i-desc').val()}
</div>
</article>
`);
});
$('#column-left').on('click', '.remove', function() {
$(this).closest('.instruction').remove();
});
const count = (arr, val) => arr.filter(n => n === val).length;
// или
const count = (arr, val) => arr.reduce((acc, n) => acc + (n === val), 0);
function Counter(data, key = n => n) {
const counted = new Map;
for (const n of data) {
const k = key(n);
counted.set(k, (counted.get(k) ?? 0) + 1);
}
return k => counted.get(k) ?? 0;
}
const arr = [ 1, 1, NaN, 1, 2, NaN, 9, NaN, NaN, 9, 7, 'hello, world!!', 'hello, world!!' ];
const counted = Counter(arr);
console.log(counted(1)); // 3
console.log(counted(NaN)); // 4
console.log(counted('hello, world!!')); // 2
console.log(counted(10)); // 0
<span class="color">red</span>
<span class="color">green</span>
<span class="color">red</span>
<span class="color">red</span>
const counted = Counter(document.querySelectorAll('.color'), el => el.innerText);
console.log(counted('red')); // 3
console.log(counted('blue')); // 0
const sortedElements = [...elements].sort((a, b) =>
+a.relations.includes(b.id) ||
-b.relations.includes(a.id) ||
a.name.localeCompare(b.name)
);
<a data-problem="value1">
<a data-problem="value2">
<a data-problem="value1|value2">
$('.problem').change(function() {
const problems = $(':checked', this)
.get()
.map(({ dataset: { type, problem } }) => ({ type, problem }));
$(this)
.closest('.remont')
.find('.price__item')
.hide()
.filter((i, { dataset: d }) =>
problems.some(p => d.type === p.type && d.problem.includes(p.problem))
)
.show();
}).change();