const array = Array.from({ length: 20 }, (v, i) =>
Math.floor(Math.random() * (10 - 1) + 1 ));
const set = new Set(array);
const result = [...set].slice(0, 5);
console.log(result);const array = Array.from({ length: 10 }, (v, i) => i)
.sort((a, b) => 0.5 - Math.random())
.slice(0, 5);
console.log(array);
const compare= (v, v2) => {
const parts = String(v).split('.')
const pow = parts.length > 1 ? parts.pop().length : 0
return Math.round(Math.abs(v - v2) * Math.pow(10, pow)) <= 1
}
console.log(compare(7.98, 7.99)) // true
console.log(compare(7.98, 7.97)) // true
console.log(compare(7.98, 8)) // false
console.log(compare(0.1, 0.15)) // trueconst compare= (v, v2) => {
const parts = [v, v2].map(v => String(v).split('.'))
const pow = Math.max(...parts.map(v => v.length > 1 ? v.pop().length : 0))
return Math.round(Math.abs(v - v2) * Math.pow(10, pow)) <= 1
}
console.log(compare(7.98, 7.99)) // true
console.log(compare(7.98, 7.97)) // true
console.log(compare(7.98, 8)) // false
console.log(compare(0.1, 0.15)) // false
Чтобы узнать подробнее, нужно иметь историю событий. Программа atop умеет вести учет процессов и ресурсов, позволяя позже проиграть историю, выяснив причину проблемы.
https://haydenjames.io/use-atop-linux-server-perfo...
https://haydenjames.io/linux-server-performance-di...
const createList = arr =>
arr.reduceRight((acc, n) => ({
val: n,
next: acc,
}), null);function createList(arr) {
let list = null;
for (let i = arr.length; i--;) {
list = {
val: arr[i],
next: list,
};
}
return list;
}const createList = (arr, i = 0) =>
i < arr.length
? ({ val: arr[i], next: createList(arr, i + 1) })
: null;
.push(x) на .push([...x]).function pyramid(n) {
const result = [];
for (let i = 0; ++i <= n;) {
const x = [];
for (let j = 0; ++j <= i; x.push(1)) ;
result.push(x);
}
return result;
}const pyramid = length => Array.from({ length }, (_, i) => Array(i + 1).fill(1));
T это generic параметр. В данном контексте это тип фильтруемого значения.(source: T): R которая еще и унарная функция итд. Это всё иерархия типов для того что бы можно было композировать друг с другом функции по их "смыслу" а не сигнатуре.
const attacks = [
{ minChance: 7, damage: 40, name: 'critical' },
{ minChance: 5, damage: 20, name: 'big' },
{ minChance: 0, damage: 10, name: 'weak' },
];
const messages = {
start: (player, enemy) => `Welcome! Yor health is - ${player}%, your enemy health - ${enemy}%`,
end: (player, enemy) => `You ${enemy <= 0 ? 'win' : 'lost'}! Your hp level - ${player}, opponents hp level - ${enemy}`,
chance: (player, enemy) => `your chance - ${player}, your opponent chance - ${enemy}`,
turn: (player, enemy, hit, isEnemy) =>
`${isEnemy ? 'Enemy' : 'Your'} turn...
${isEnemy ? 'Enemy' : 'You'} did a ${hit} hit
${isEnemy ? 'Your' : 'Enemy'} hp - ${isEnemy ? player : enemy}`,
};
const simpleFight = () => {
const hp = [ 100, 100 ];
console.log(messages.start(...hp));
while (hp.every(n => n > 0)) {
const chances = hp.map(() => Math.random() * 11 | 0);
console.log(messages.chance(...chances));
if (chances[0] !== chances[1]) {
const chance = Math.max(...chances);
const attack = attacks.find(n => n.minChance < chance);
const isEnemyAttacks = chance === chances[1];
hp[+!isEnemyAttacks] -= attack.damage;
console.log(messages.turn(...hp, attack.name, isEnemyAttacks));
}
}
console.log(messages.end(...hp));
};