function translatePigLatin(str) {
const x = str.search(/[aioue]/);
return x > 0
? `${str.slice(x)}${str.slice(0, x)}ay`
: `${str}${x ? '' : 'w'}ay`;
}
function myReplace(str, before, after) {
if (before[0].toUpperCase() === before[0]) {
after = after[0].toUpperCase() + after.slice(1);
}
return str.replace(before, after);
}
const sumNested = (data, getVal, key) => Object
.entries(data instanceof Object ? data : {})
.reduce((acc, [ k, v ]) => acc + sumNested(v, getVal, k), getVal(key, data));
const numItems = sumNested(obj, (k, v) => (k === 'items' && Array.isArray(v)) ? v.length : 0);
function sumNested(data, getVal) {
let result = 0;
for (const stack = [ [ , data ] ]; stack.length;) {
const [ k, v ] = stack.pop();
stack.push(...(v instanceof Object ? Object.entries(v) : []));
result += getVal(k, v);
}
return result;
}
final_data.reduce((acc1, num1) => acc1.reduce((acc2, num2) => acc2.concat(num1.map(num3 => [].concat(num2, num3))), []))
final_data.reduce((acc1, num1) => {
return acc1.reduce((acc2, num2) => {
acc2.push(...num1.map(num3 => [...num2, ...num3]));
return acc2;
}, [])
})
const whatIsInAName = (collection, source) =>
collection.filter(function(n) {
return this.every(([ k, v ]) => n[k] === v);
}, Object.entries(source));
const WRAP = 4;
const $items = $('.wrap > .item');
for (let i = 0; i < $items.length; i += WRAP) {
$items.slice(i, i + WRAP).wrapAll('<div class="wrap-item">');
}
const WRAP = 4;
const container = document.querySelector('.wrap');
const items = [...container.children];
container.append(...Array.from(
{ length: Math.ceil(items.length / WRAP) },
(n, i) => {
const wrapper = document.createElement('div');
wrapper.classList.add('wrap-item');
wrapper.append(...items.slice(i * WRAP, (i + 1) * WRAP));
return wrapper;
}
));
const text = document.querySelector('#area').value;
const pre = document.querySelector('#pre');
pre.innerHTML = text.replace(/f+/g, '<span>$&</span>');
document.addEventListener('scroll', function() {
document.querySelector('.wrap_portfolio').style.transform = `translateX(${window.scrollY}px)`;
});
const color = 'orange';
const { id } = Object.values(fruits).find(n => n.color === color) || {};
$('[data-index]').click(function() {
$('#main__display').html(`<img src="${collect[this.dataset.index]}">`);
});
document.querySelectorAll('[data-index]').forEach(function(n) {
n.addEventListener('click', this);
}, function(e) {
this.innerHTML = `<img src="${collect[e.target.dataset.index]}">`;
}.bind(document.querySelector('#main__display')));
const THICKNESS = {
thinLine: 1,
get midLine() {
return this.thinLine * 2;
},
get boldLine() {
return this.thinLine * 4;
},
};
после 21 итерации выдает неправильное значение даты
const startDate = new Date(2019, 7, 11);
const currentDate = new Date(startDate);
for (let i = 0; i <= 30; i++) {
currentDate.setDate(currentDate.getDate() + 1);
console.log(currentDate);
}
const startDate = new Date(2019, 7, 11);
for (let i = 0; i <= 30; i++) {
const currentDate = new Date(startDate);
currentDate.setDate(startDate.getDate() + i);
console.log(currentDate);
}
function createList(data) {
const list = document.createElement('ul');
const stack = [];
let level = +data[0].match(/\d+/);
let ul = list;
for (const n of data) {
const currLevel = +n.match(/\d+/);
if (currLevel > level) {
stack.push([ ul, level ]);
[ ul, level ] = [ document.createElement('ul'), currLevel ];
stack[stack.length - 1][0].lastElementChild.append(ul);
} else {
for (; currLevel !== level; [ ul, level ] = stack.pop()) ;
}
ul.insertAdjacentHTML('beforeend', `<li>${n}</li>`);
}
return list;
}
изучаю метод фильтр
greater than or equal to 8.0
rating > 8
combination of filter and map
watchList.filter(n => n.imdbRating >= 8).map(n => ({ title: n.Title, rating: n.imdbRating }))
.one path {
fill: #909090;
}
.one.active path {
fill: #f00;
}
const itemSelector = '.one';
const activeClass = 'active';
document.addEventListener('click', function(e) {
const el = e.target.closest(itemSelector);
if (el) {
el.classList.toggle(activeClass);
}
});
// или
document.querySelectorAll(itemSelector).forEach(function(n) {
n.addEventListener('click', this);
}, e => e.currentTarget.classList.toggle(activeClass));