const parent = document.querySelector('#questions');
const selector = '#point';const children = [...parent.children];
const index = -~children.findIndex(n => n.matches(selector));
index && parent.replaceChildren(...children.slice(0, index));
// или
for (
const el = parent.querySelector(selector);
el?.nextElementSibling;
el.nextElementSibling.replaceWith()
) ;
// или
parent.querySelectorAll(`${selector} ~ *`).forEach(n => n.remove());
document.querySelector('.shopWrapper').addEventListener('mouseover', function() {
const color = `#${Math.random().toString(16).slice(2, 8).padEnd(6, 0)}`;
this.style.setProperty('--random-color', color);
});
arr.map(n => {
const ids = Object
.values(n.childrenHash.reduce((acc, m) => ((acc[m.hash] ??= []).push(m.id), acc), {}))
.filter(m => m.length > 1);
return {
...n,
childrenHash: ids.length ? ids : null,
};
})
const table = document.querySelector('здесь селектор вашей таблицы');
const className = 'active';table.querySelectorAll('tbody td').forEach(td => {
td.classList.toggle(className, !td.textContent.trim());
});for (const { rows } of table.tBodies) {
for (const { cells } of rows) {
for (const td of cells) {
if (/^\s*$/.test(td.innerText)) {
td.classList.add(className);
}
}
}
}
const where = '.container';
const what = '.wrapper-item h3';$(where).prepend(function() {
return $(what, this);
});document.querySelectorAll(where).forEach(n => {
const el = n.querySelector(what);
n.prepend(el);
// или
n.insertBefore(el, n.firstChild);
// или
n.insertAdjacentElement('afterbegin', el);
// или
n.firstChild.replaceWith(el, n.firstChild);
// или
n.replaceChildren(el, ...n.childNodes);
});
const index = 1;
const className = 'active';navigation.children[index]?.classList.add(className);navigation.innerHTML = objectNavigation
.map((n, i) => `
<div class="${i === index ? className : ''}">
<img src="${n.image}">
</div>`)
.join('');navigation.append(...objectNavigation.map((n, i) => {
const div = document.createElement('div');
const img = document.createElement('img');
img.src = n.image;
div.append(img);
div.classList.toggle(className, i === index);
return div;
}));for (const [ i, n ] of objectNavigation.entries()) {
navigation.appendChild(document.createElement('div'));
navigation.lastChild.appendChild(new Image);
navigation.lastChild.lastChild.src = n.image;
navigation.lastChild.className = i === index ? className : '';
}
const classes = {
Class1: class {
constructor(val) {
this.val = val;
}
method1() {
console.log('Class1', this.val);
}
},
Class2: class {
constructor(val1, val2) {
this.val1 = val1;
this.val2 = val2;
}
method2() {
console.log('Class2', this.val1, this.val2);
}
},
};
function createInstanceAddCallMethod(className, constructorParams, methodName) {
const instance = new classes[className](...constructorParams);
instance[methodName]();
}
createInstanceAddCallMethod('Class1', [ 69 ], 'method1');
createInstanceAddCallMethod('Class2', [ 187, 666 ], 'method2');
const newArray = numbers.filter(i => i !== firstMin);
function sumTwoSmallestNumbers(nums) {
const iMin = nums.indexOf(Math.min(...nums));
return nums[iMin] + Math.min(...nums.filter((_, i) => i !== iMin));
}const sumTwoSmallestNumbers = ([...nums]) => nums
.sort((a, b) => b - a)
.slice(-2)
.reduce((acc, n) => acc + n, 0);function sumTwoSmallestNumbers(nums) {
const count = Object.entries(nums.reduce((acc, n) => (acc[n] = -~acc[n], acc), {}));
return +count[0][0] + +count[+(count[0][1] === 1)][0];
}function sumTwoSmallestNumbers(nums) {
let min1 = Infinity;
let min2 = Infinity;
for (const n of nums) {
if (n < min1) {
[ min1, min2 ] = [ n, min1 ];
} else if (n < min2) {
min2 = n;
}
}
return min1 + min2;
}
// или
const sumTwoSmallestNumbers = nums =>
eval(nums.reduce(([ min1, min2 ], n) =>
n < min1 ? [ n, min1 ] :
n < min2 ? [ min1, n ] :
[ min1, min2 ]
, [ Infinity, Infinity ]).join('+'));
const mul = arr =>
arr.reduce((acc, n) => acc * n, 1);
// или
const mul = arr =>
eval(arr.join('*')) ?? 1;
// или
function mul(arr) {
let result = 1;
for (const n of arr) {
result *= n;
}
return result;
}
// или
function mul(arr) {
let result = 1;
for (let i = 0; i < arr.length; i++) {
result = result * arr[i];
}
return result;
}
// или
const mul = (arr, i = 0) =>
i < arr.length
? arr[i] * mul(arr, -~i)
: 1;const result = count.map(function(n) {
return mul(data.slice(this[0], this[0] += n));
}, [ 0 ]);const result = count.map(n => mul(data.splice(0, n)));
function combine(a = {}, b = {}, c = {}) {const combine = (...arr) => arr
.flatMap(Object.entries)
.reduce((acc, [ k, v ]) => (acc[k] = (acc[k] ?? 0) + v, acc), {});function combine() {
const result = {};
for (const n of arguments) {
for (const k in n) {
if (n.hasOwnProperty(k)) {
if (!result.hasOwnProperty(k)) {
result[k] = 0;
}
result[k] += n[k];
}
}
}
return result;
}
const mostFrequentNum = Array
.from(arr.reduce((acc, n) => acc.set(n, -~acc.get(n)), new Map))
.reduce((max, n) => max[1] > n[1] ? max : n, [ , 0 ])
.at(0);const mostFrequentNum = Object
.entries(arr.reduce((acc, n) => (acc[n] = (acc[n] ?? 0) + 1, acc), {}))
.reduce((acc, n) => (acc[n[1]] = +n[0], acc), [])
.pop();
let [0: [fruit1, fruit2, fruit3]] = arr; //Uncaught SyntaxError: Invalid destructuring assignment target
const arr = [
[ 1, 2, 3 ],
[ 5, 6, 7 ],
[ 7, 8, 9 ],
];
const [ , [ , val1 ], [ ,, val2 ] ] = arr;
console.log(val1, val2); // 6 9const arr = [
[ 1, 2, 3 ],
[ 5, 6, 7 ],
[ 7, 8, 9 ],
];
const { 1: { 1: val1 }, 2: { 2: val2 } } = arr;
console.log(val1, val2); // 6 9
Object.values(t).forEach(n => {
n.children?.sort((a, b) => (a.order - b.order) || a.name.localeCompare(b.name));
});
const square = n => n ** 2;
// или
const square = n => n * n;
// или
const square = n => Math.pow(n, 2);arr.forEach((n, i, a) => a[i] = square(n));
// или
arr.splice(0, arr.length, ...arr.map(square));
// или
for (const [ i, n ] of arr.entries()) {
arr[i] = square(n);
}
// или
for (let i = 0; i < arr.length; i++) {
arr[i] = square(arr[i]);
}
Как мне изменить код, чтобы исправить ошибку?
const countries = Array.from({ length: 3 }, prompt);
new gridjs.Grid({
columns: [
'Code',
{ name: 'Flag', formatter: val => gridjs.html(`<img src="${val}">`) },
'Name',
'Capital',
'Population',
],
data: () => Promise.all(countries.map(n =>
fetch(`//restcountries.com/v3.1/name/${n}`)
.then(r => r.json())
.then(([ r ]) => [
r.altSpellings[0],
r.flags.png,
r.name.common,
r.capital[0],
r.population,
])
.catch(() => [ ,,`${n} - это не страна`,, ])
)),
}).render(document.querySelector('#wrapper'));
(str.match(/[0-9]+/g) ?? []).map(Number)
// или
Array.from(str.matchAll(/\d+/g), n => +n)
// или
(function get(arr, i, n = arr[i]) {
return i < arr.length
? (n ? [ parseInt(n) ] : []).concat(get(arr, -~i))
: [];
})(str.split(/[^\d]+/), 0)
// или
eval(`[${str.replace(/\D+/g, (m, i) => i ? ',' : '')}]`)
ищет метод toString - его нет
test.toString, то результатом будет undefined. Вы проверьте, так ли это. Будете удивлены.