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 }))
// или
watchList.reduce((acc, { Title: title, imdbRating: rating }) => (
rating < 8 || acc.push({ title, rating }),
acc
), [])
.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));
const result = arrs.reduce(
(acc, arr) => (arr.forEach((n, i) => acc[i].push(...n.slice(acc[i].length))), acc),
Array.from({ length: Math.max(...arrs.map(arr => arr.length)) }, () => [])
);
const result = arrs.reduce((acc, arr) => (
arr.forEach((n, i) => (
acc[i] = acc[i] || [],
n.forEach((m, j) => acc[i][j] = acc[i][j] || m)
)),
acc
), []);
Метод Performance.now()
возращает временную метку DOMHighResTimeStamp, измеряемую в миллисекундах
const createArr = (len, digit, count) => {
const d = [...Array(9)].map((n, i) => i + (i >= digit));
return [...Array(len)].map(() => {
const number = [...Array(Math.random() * 10 | 0)].map(() => d[Math.random() * d.length | 0]);
for (let i = 0; i < count; i++) {
number.splice(Math.random() * number.length | 0, 0, digit);
}
return +number.join('');
});
};
const arr = createArr(5, 1, 2);
const tbody = document.querySelector('#test tbody');
const keys = [ 'name.firstName', 'name.lastName', 'about', 'eyeColor' ].map(n => n.split('.'));
const getVal = (obj, keys) => keys.reduce((p, c) => p != null ? p[c] : p, obj);
for (const n of data) {
keys.forEach(function(k) {
this.insertCell().textContent = getVal(n, k);
}, tbody.insertRow());
}
// или
tbody.insertAdjacentHTML('beforeend', data
.map(n => `
<tr>${keys.map(k => `
<td>${getVal(n, k)}</td>`).join('')}
</tr>`)
.join('')
);
AutoNumeric.multiple('.numeric', {
decimalCharacter : '.',
digitGroupSeparator : ' ',
})
<div id="images">
<img src="https://placehold.co/200x200/FF0000/FFFFFF/png">
<img src="https://placehold.co/200x200/00FF00/000000/png">
<img src="https://placehold.co/200x200/0000FF/FFFFFF/png">
</div>
#images img {
transition: transform 0.7s;
}
.rotate {
transform: rotateY(180deg);
}
const toggleRotate = el => el.classList.toggle('rotate');
document.querySelectorAll('#images img').forEach((n, i) => {
setTimeout(setInterval, 300 * i, toggleRotate, 2000, n);
});
#images img {
animation: rotate 4s infinite;
}
#images img:nth-child(1) { animation-delay: 0s; }
#images img:nth-child(2) { animation-delay: 0.3s; }
#images img:nth-child(3) { animation-delay: 0.6s; }
@keyframes rotate {
0%, 75%, 100% {
transform: rotateY(0deg);
}
25%, 50% {
transform: rotateY(180deg);
}
}
messages.sort((a, b) => {
[ a, b ] = [ a, b ].map(n => sortableLanguages.indexOf(n.language));
return a === -1 ? 1 : b === -1 ? -1 : a - b;
});
const sorted = (arr, key) => arr
.map(n => [ n, key(n) ])
.sort((a, b) => a[1] - b[1])
.map(n => n[0]);
const order = Object.fromEntries(sortableLanguages.map((n, i) => [ n, i + 1 ]));
const sortedMessages = sorted(messages, n => order[n.language] || Number.MAX_SAFE_INTEGER);
// или
function sorted(arr, order, key) {
const ordered = new Map(order.map(n => [ n, [] ]));
const unordered = [];
arr.forEach(n => (ordered.get(key(n)) || unordered).push(n));
return [].concat(...ordered.values(), unordered);
}
const sortedMessages = sorted(messages, sortableLanguages, n => n.language);
const buttonSelector = '.one';
const contentSelector = '.two';
const className = 'three';
document.querySelector(buttonSelector).addEventListener('click', () => {
document.querySelector(contentSelector).classList.toggle(className);
});
document.addEventListener('click', e => {
const button = e.target.closest(buttonSelector);
if (button) {
const index = [...document.querySelectorAll(buttonSelector)].indexOf(button);
document.querySelectorAll(contentSelector)[index].classList.toggle(className);
}
});
// или
const buttons = document.querySelectorAll(buttonSelector);
const contents = document.querySelectorAll(contentSelector);
buttons.forEach(n => n.addEventListener('click', onClick));
function onClick(e) {
const index = Array.prototype.indexOf.call(buttons, e.currentTarget);
contents[index].classList.toggle(className);
}
for (let i = 0; i < password.length; i + 1) {
если запустить код то страница крашнется, codesandbox на счет этого говорит что циклов слишком много
i + 1
- счётчик не меняет своего значения, цикл получается бесконечным.i += 1
или i++
.return pass;
return text
. const mustStay = arr => arr.some(n => n !== '');
const newArr = arr.filter(mustStay);
arr.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), null);
// или
arr.splice(0, arr.length, ...arr.filter(mustStay));
// или
let numDeleted = 0;
for (const [ i, n ] of arr.entries()) {
arr[i - numDeleted] = n;
numDeleted += !mustStay(n);
}
arr.length -= numDeleted;
[...divs.keys()].forEach(i => console.log(i));
// или
console.log(Array.from(divs.keys(), i => i * 100));
// или
for (const i of divs.keys()) {
console.log(i);
}