const phones = [
{ p: '9171857450', c: 1 },
{ p: '9880735438', c: 10 },
{ p: '9880735439', c: 100 },
{ p: '9880735779', c: 2 },
{ p: '9170997305', c: 2 },
{ p: '9170997493', c: 2 },
{ p: '9880634879', c: 5 },
{ p: '9170996154', c: 1 },
{ p: '9880728447', c: 1 },
];
const chancesTotal = phones.reduce((acc, { c }) => acc + c, 0);
const selected = Math.floor(Math.random() * chancesTotal);
let phone;
for (let i = 0, sum = 0; i < phones.length; i++) {
sum += phones[i].c;
if (selected < sum) {
phone = phones[i].p;
break;
}
}
const randomUrl = `https://wa.me/${phone}?text=Привет!%20Пришлите%20мне%20цены%20на%20рыбку!`;
const makeDate = HHMM => {
const [H, M] = HHMM.split(':').map(Number);
const D = new Date();
D.setHours(H);
D.setMinutes(M);
D.setSeconds(0);
return D;
};
const oo = n => n.toString().padStart(2, '0'); // 5 => '05'
const fillTime = (startHHMM, finishHHMM, intervalMinutes) => {
let startDate = makeDate(startHHMM);
let finishDate = makeDate(finishHHMM);
if (startDate > finishDate) { // объекты Date можно так сравнивать
[startDate, finishDate] = [finishDate, startDate]; // поменять местами
}
const dates = [];
const D = new Date(startDate);
while (D <= finishDate) {
dates.push(new Date(D));
D.setMinutes(D.getMinutes() + intervalMinutes);
}
return dates.map(D => [D.getHours(), D.getMinutes()].map(oo).join(':'));
};
console.log(fillTime('15:56', '18:15', 17));
// [ "15:56", "16:13", "16:30", "16:47", "17:04", "17:21", "17:38", "17:55", "18:12" ]
const groupSum = (arr, idKey, ...sumKeys) =>
Object.values(arr.reduce((acc, n) => (
acc[n[idKey]] ??= sumKeys.reduce((group, k) => (group[k] = 0, group), { ...n }),
sumKeys.forEach(k => acc[n[idKey]][k] += n[k]),
acc
), {}));
const result = groupSum(arr, 'product', 'price', 'selling_price', 'quantity');
возвращает массив индексов элементов, у которые значение равно value.
const findIndex = (arr, value) => arr
.map((v, i) => ({ v, i }))
.filter(({ v }) => v === value)
.map(({ i }) => i);
Каждый элемент массива переделать в объект, где v
: элемент, а i
: индекс в массиве.бывш.элемент === value
i
const textDecoder = new TextDecoder('windows-1251');
const response = await fetch(...);
const buffer = await response.arrayBuffer();
const text = textDecoder.decode(buffer);
(async function() {
var result = await $.post('api/index.php', { raz: 'Привет, мир!' } );
console.log('Ответ сервера:', result);
})();
console.log
$_POST
<?php
header('Content-Type: text/plain; charset=UTF-8');
$json = json_encode($_POST, JSON_UNESCAPED_UNICODE);
file_put_contents('POST.json', $json);
echo 'Данные получены и записаны в файл POST.json -> смотри его.';
.hint
) пересекается с блоком А (основным текстом), и после, с использованием полученного и порогового (задаёте сами), можно решить, что делать с блоком.const clamp = (min, max, value) => Math.max(min, Math.min(max, value));
const calculateIntersection = (a, b) => {
const aRect = a.getBoundingClientRect();
const bRect = b.getBoundingClientRect();
const top = clamp(aRect.top, aRect.bottom, bRect.top);
const right = clamp(aRect.left, aRect.right, bRect.right);
const bottom = clamp(aRect.top, aRect.bottom, bRect.bottom);
const left = clamp(aRect.left, aRect.right, bRect.left);
const width = right - left;
const height = bottom - top;
const totalArea = bRect.width * bRect.height;
const intersectionArea = width * height;
const intersectionRatio = intersectionArea / totalArea;
return intersectionRatio;
};
<form action="receive_data.php" method="post">
<input name="username">
<button type="submit">Send</button>
</form>
<?php
$name = $_POST['username'];
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello '.$name;
$headers = 'From: webmaster@example.com';
mail($to, $subject, $message, $headers);
header('Location: /other_page.php');
function search(nums, target, searchMin) {
for (let iMin = 0, iMax = nums.length - 1; iMin <= iMax; ) {
const index = (iMin + iMax) / 2 | 0;
const num = nums[index];
if (num === target && num !== nums[index + (searchMin ? -1 : 1)]) {
return index;
} else if (num <= target - (searchMin ? 1 : 0)) {
iMin = index + 1;
} else {
iMax = index - 1;
}
}
return -1;
}
function searchMinMax(nums, target) {
const iMin = search(nums, target, true);
return iMin === -1
? []
: [ iMin, search(nums, target, false) ];
}