.
начало
---|---|---
к | 1
о -
н | 2 1
е -
ц | 3 2 1
if - else if - else
function ranges(int $a, int $b): array
{
$start = 20;
$finish = 30;
$result = [
'before' => null,
'match' => null,
'after' => null,
];
if ($a < $start) {
if ($b < $start) {
$result['before'] = [$a, $b];
} else {
$result['before'] = [$a, $start];
if ($b < $finish) {
$result['match'] = [$start, $b];
} else {
$result['match'] = [$start, $finish];
$result['after'] = [$finish, $b];
}
}
} else if ($a < $finish) {
if ($b < $finish) {
$result['match'] = [$a, $b];
} else {
$result['match'] = [$a, $finish];
$result['after'] = [$finish, $b];
}
} else {
$result['after'] = [$a, $b];
}
return $result;
}
print(json_encode(ranges(5, 25))); // {"before":[5,20],"match":[20,25],"after":null}
$arr = [$start, $finish, $a, $b];
sort($arr, SORT_NUMERIC);
const dateInterval = [new Date(), new Date()];
const setTimes = interval =>
interval
.map(d => new Date(d))
.map((d, i) => {
d.setHours(23 * i);
d.setMinutes(59 * i);
d.setSeconds(59 * i);
d.setMilliseconds(999 * i);
return d;
});
setTimes(dateInterval)
// Array [
// Date Mon Nov 07 2022 00:00:00 GMT+0300 (Moscow Standard Time),
// Date Mon Nov 07 2022 23:59:59 GMT+0300 (Moscow Standard Time)
// ]
const { searchParams } = new URL(document.location.href);
const url = new URL('https://ya.ru');
['utm_source', 'utm_medium', 'utm_campaign']
.filter(param => searchParams.has(param))
.forEach(param => url.searchParams.set(param, searchParams.get(param)));
const urlString = url.toString();
document.querySelectorAll('div.tg-button > a')
.forEach(el => el.setAttribute('href', urlString));
Получить объект параметр-значение из адреса текущей страницы;1, 2, 3, 2, 3, 2, 1
0 + + - + - -
^ ^ ^ в этих точках знак поменялся
Складывать найденные точки в два массива: максимумы и минимумы.const target = { a: '', b: 'V'}; // скопируем сюда
const source = { a: 'AAA', c: 'CCC' }; // отсюда
Object.assign(target, source);
target // { a: "AAA", b: "V", c: "CCC" }
participants
, то можно так:hotel.forEach(({ room }) => room.forEach(slot => Object.assign(slot, participants.pop())));
participants
.let i = 0;
hotel.forEach(({ room }) =>
room.forEach((slot) => Object.assign(slot, participants[i++]))
);
function f() {}; // традиционное объявление функции
тут f
вылезет наверх ("hoisting") и будет доступна всему коду.(function f() {}) // функциональное выражение function expression
// ещё варианты
const x = function f() {};
console.log( function f() {} );
имя f
будет относиться к этой функции только внутри неё, для рекурсивных вызовов, например.if( function() {})
как раз создаёт function expression. Поэтому f
нигде снаружи не видна. value = 1000 + 135 * (15000 - 1000) / 180 = 11500
pattern="\+49[0-9]{10,11}"
showError()
надо проверять свойство patternMismatch: } else if (phone.validity.patternMismatch) {
phoneError.textContent = 'phone should be +49 followed by 10 to 11 digits';
}
undefined
, старые методы пропускают.const arr = [1,2,3,4,5,6,7,8,9];
delete arr[1];
delete arr[3];
delete arr[5];
// arr == [ 1, <1 empty slot>, 3, <1 empty slot>, 5, <1 empty slot>, 7, 8, 9 ]
const present = [];
arr.forEach((_, i) => present.push(i));
// present == [ 0, 2, 4, 6, 7, 8 ]
const empty = Array.from({ length: arr.length }, (_, i) => i) // [0,1,2,3,4,5,6,7,8]
.filter(i => !present.includes(i));
// empty == [ 1, 3, 5 ]
X = 13 % 6 = 1
Y = floor(13 / 6) = 2
function generator(quantity, side = 6) {
const length = side * side;
if (quantity > length) throw new Error('too much u ask');
const options = Array.from({ length }, (_, i) => i); // [0,1,2,3, .. ,35]
const nToPoint = n => ({ x: n % side, y: Math.floor(n / side) }); // 13 => {x:1, y:2}
return Array.from({ length: quantity }, () =>
nToPoint(
options.splice(Math.floor(Math.random() * options.length), 1).pop()
)
);
}
console.log(generator(4));
// [{x:1,y:2}, {x:0,y:4}, {x:1,y:0}, {x:4,y:4}]
0..8
как div'ы в контейнере.textpayload = message.user.bonusNew ? `"{\"button\": \"дуэль\"}"` : "{\"button\": \"бонус_новичка\"}`"
`
const bonusButton = { button: message.user.bonusNew ? 'дуэль' : 'бонус_новичка' };
textpayload = JSON.stringify(bonusButton);