await Promise.all([
'https://learn.javascript.ru/article/promise-chaining/one.js',
'https://learn.javascript.ru/article/promise-chaining/two.js',
'https://learn.javascript.ru/article/promise-chaining/three.js',
].map(loadScript));
one();
two();
three();
function siftDown(arr, i) {
let chi = i * 2 + 1;
while (chi < arr.length) {
const minChildIdx = chi < arr.length - 1 && arr[chi + 1] < arr[chi] ? chi + 1 : chi;
const chv = arr[minChildIdx];
if (arr[i] <= chv) break;
arr[minChildIdx] = arr[i];
arr[i] = chv;
i = minChildIdx;
chi = i * 2 + 1;
}
}
function heapify(arr) {
for (let i = Math.floor(arr.length / 2); i >= 0; --i) {
siftDown(arr, i);
}
}
function findMinWithHeap(arr) {
const heap = arr.flat();
heapify(heap);
return heap[0];
}
console.log(findMinWithHeap([[2, 1, 2], [0, 4, 5] ]));
function sortTickets(tickets) {
const map = new Map();
const transits = new Set();
tickets.forEach((t) => {
map.set(t.from, t);
transits.add(t.to);
});
const start = tickets.find(t => !transits.has(t.from));
const result = [];
for (let t = start; t; t = map.get(t.to)) {
result.push(t);
}
return result;
}
const sorted = sortTickets([
{from: 'Sochi', to: 'Paris'},
{from: 'Moscow', to: 'Sochi'},
{from: 'London', to: 'Moscow'},
{from: 'Berlin', to: 'Kamchatka'},
{from: 'Paris', to: 'Berlin'},
]);
class ClassCounter extends React.Component {
...
increment = () => {
console.log(this);
this.setState({ count: this.state.count + 1 });
};
class ClassCounter extends React.Component {
constructor(props) {
super(props);
this.increment = () => {
console.log(this);
this.setState({ count: this.state.count + 1 });
};
}
Напишите функцию replacer для JSON-преобразования, которая удалит свойства, ссылающиеся на meetup
function makeReplacer() {
const path = [];
return (k, v) => {
// простое значение не записываем в путь
if (!v || typeof v !== 'object') {
return v;
}
// находим родительский объект; всё что глубже, выкидываем
for (let i = path.length - 1; i >= 0; --i) {
const item = path[i];
if (Object.prototype.hasOwnProperty.call(item.obj, k) && item.obj[k] === v && !item.handled.has(k)) {
item.handled.add(k);
break;
} else {
path.pop();
}
}
// если в парентах уже был текущий объект, то игнорируем его
if (path.find((item) => item.obj === v)) {
return undefined;
}
// записываем объект в путь и возвращаем
path.push({
obj: v,
handled: new Set()
});
return v;
};
}
// -----------------------------
// пример использования
let room = {
number: 23
};
let obj1 = {a: {b: room, bb: 23}, c: 34};
let meetup = {
title: "Совещание",
occupiedBy: [room, {name: "Иванов"}, {name: "Петров"}],
place: room
};
room.occupiedBy = meetup;
meetup.self = meetup;
room.self = room;
room.obj1 = obj1;
console.log(JSON.stringify(meetup, makeReplacer(), 4))