function format(str) {
const s = str.length;
const chars = str.split('');
const strWithSpaces = chars.reduceRight((acc, char, i) => {
const spaceOrNothing = ((((s - i) % 3) === 0) ? ' ' : '');
return (spaceOrNothing + char + acc);
}, '');
return ((strWithSpaces[0] === ' ') ? strWithSpaces.slice(1) : strWithSpaces);
}
const arr = [
[1, 'Вася'],
[2, 'Петя'],
[3, 'Коля'],
[4, 'Федя']
];
const obj = arr.reduce((obj, innerArr) => {
obj[innerArr[0]] = innerArr[1];
return obj;
}, {});
console.log(b[1]); // 2: 'Петя'
for (let user in b) { console.log(user); } // 1, 2, 3, 4
sha-1
всегда возвращает 160 бит, что равно 20 байтам. Байт обычно записывают с помощью 2 шестнадцатеричных цифр, по другому его отобразить сложно. В итоге получается 40 знаков./(sh|sch|ch|cz|yu|ya|yo|zh|''|'|.)/
^
// └ Сопоставляется с любым символом
/(sh|sch|ch|cz|yu|ya|yo|zh|''|')/
class Animal {}
class Cat {}
const animal = new Animal();
const cat = new Cat();
console.log(animal instanceof Animal); // true
console.log(animal instanceof Cat); // false
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true
Reflect.setPrototypeOf(Cat.prototype, Animal.prototype);
console.log(animal instanceof Animal); // true
console.log(animal instanceof Cat); // false
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true
function findSum(arr) {
const arrSum = arr.map(subArr => subArr.flat(Infinity).reduce((subSum, item) => (subSum + item)));
return arrSum;
}
const rawObject = {
exchangeRate: [
{
baseCurrency: 'UAH',
currency: 'EUR',
saleRateNB: 18.7949200,
purchaseRateNB: 18.7949200,
saleRate: 20.0000000,
purchaseRate: 19.2000000
},
{
baseCurrency: 'UAH',
currency: 'GEL',
saleRateNB: 8.1500890,
purchaseRateNB: 8.1500890
},
{
baseCurrency: 'UAH',
currency: 'PLZ',
saleRateNB: 4.4922010,
purchaseRateNB: 4.4922010,
saleRate: 5.0000000,
purchaseRate: 4.2000000
}
]
};
const euro = rawObject.exchangeRate.find(obj => (obj.currency === 'EUR'));
console.log(euro);
console.log(`Euro saleRate: ${euro.saleRate}`);
console.log(`Euro purchaseRate: ${euro.purchaseRate}`);
const player1 = document.getElementById('player1');
const player2 = document.getElementById('player2');
const resultElem = document.getElementById('result');
const btn = document.getElementById('play');
btn.addEventListener('click', runGame);
function getPlayerResult() {
const result = Math.floor(3 * Math.random());
return result;
}
function getNameById(id) {
let name = 'Error';
switch (id) {
case 0:
result = 'Rock';
break;
case 1:
result = 'Scissors';
break;
case 2:
result = 'Paper';
break;
}
return name;
}
function determineWinner(idFirst, idSecond) {
const result = (idSecond - idFirst + 3) % 3;
return result;
}
function getMessageByResult(result) {
let message = 'Error';
switch (id) {
case 0:
message = 'The game ended in a draw.';
break;
case 1:
message = 'The first player won.';
break;
case 2:
message = 'The second player won.';
break;
}
return message;
}
function runGame() {
const result1 = getPlayerResult();
const result2 = getPlayerResult();
player1.innerHTML = getNameById(result1);
player2.innerHTML = getNameById(result2);
const winner = determineWinner(result1, result2);
resultElem.innerHTML = getMessageByResult(winner);
}
function toStringOrRegExp(s) {
if (s.startsWith("'") && s.endsWith("'")) {
return s.slice(1, -1);
}
if (s.startsWith("/") && s.endsWith("/")) {
try {
const regexp = new RegExp(s.slice(1, -1));
return regexp;
} catch (e) {
return s;
}
}
return s;
}
const raw = "['+', '7', '(', /\d/, /\d/, /\d/, ')', ' ', /\d/, /\d/, /\d/, '-', /\d/, /\d/, '-', /\d/, /\d/]";
const arr = raw.slice(1, -1).split(', ').map(toStringOrRegExp);
console.log(arr);
// ["+", "7", "(", /d/, /d/, /d/, ")", " ", /d/, /d/, /d/, "-", /d/, /d/, "-", /d/, /d/]
function handler(e) {
// ...
}
document.addEventListener('event-type', event => {
if (event.target.classList.contains('my-class')) {
handler(event);
}
});
function qSort(array, comparator, divider) {
if (array.length < 2) {
return array;
}
const defaultComparator = (a, b) => ((a < b) ? -1 : ((a > b) ? 1 : 0));
const defaultDivider = array => array[Math.floor(array.length * Math.random())];
const getOrder = comparator || defaultComparator;
const getPivot = divider || defaultDivider;
const pivot = getPivot(array);
const less = [], equal = [], greater = [];
for (const item of array) {
const order = getOrder(item, pivot);
const subArray = ((order < 0) ? less : ((order > 0) ? greater : equal));
subArray.push(item);
}
return [...qSort(less, getOrder, getPivot), ...equal, ...qSort(greater, getOrder, getPivot)];
}