почему ошибку выдает?
It should take an array of numbers as its argument and return the two highest numbers within the array.
function twoOldestAges($ages) {
sort($ages);
return array_slice($ages, -2);
}
function twoOldestAges($ages) {
$a = -INF;
$b = -INF;
foreach ($ages as $n) {
if ($n > $a) {
$b = $a;
$a = $n;
} else if ($n > $b) {
$b = $n;
}
}
return [ $b, $a ];
}
str.replace(/(?<=\S)(?=[А-ЯЁ])/g, ' ')
str.replace(/(\S)([А-ЯЁ])/g, '$1 $2')
// вспомогательная функция
function countDig(n, pow, pow10, pow9) {
const dig = n / pow10;
const count = dig < 6n ? dig : dig - 1n;
return count * pow9 + (dig === 5n || pow < 1n ? 0n : countDig(n % pow10, pow - 1n, pow10 / 10n, pow9 / 9n));
}
// количество беспятёрочников на отрезке [0...n-1]
function countWithout5(n) {
let pow = 0n, pow10 = 1n;
while (pow10 * 10n <= n) {
pow++;
pow10 = pow10 * 10n;
}
return countDig(n, pow, pow10, 9n ** pow);
}
function count(min, max) {
if (min > max) {
return 0n;
}
if (min >= 0n) {
return countWithout5(max + 1n) - countWithout5(min);
}
if (max <= 0n) {
return countWithout5(1n - min) - countWithout5(-max);
}
return countWithout5(max + 1n) + countWithout5(1n - min) - 1n;
}
count(4n, 17n); // 12n
count(-3455534n, 1731643265265475472546254726454363145657453757347547n);
// результат 7912695757329425999049503116097171379949505558054n
const computed = results.reduce(
(acc, item) => (item.data.forEach(({ name, value }) => (acc[name] = (acc[name] ?? 0) + value)), acc),
{}
);
const personInfo = {
labels: Object.keys(computed),
values: Object.values(computed)
};
const result = arr.flat();
// или
const result = Array.prototype.concat.apply([], arr);
// или
const result = arr.reduce((acc, n) => (acc.push(...n), acc), []);
// или
const result = [];
for (const n of arr) {
for (const m of n) {
result[result.length] = m;
}
}
async function() {
let result = null;
while (1) {
result = await fetch(...);
if (result тот, который нужен) {
break;
}
await new Promise(r => setTimeout(r, 5000));
}
return result;
}
const elements = document.querySelectorAll('[name^=AR_AMOUNT]');
const sum = Array.prototype.reduce.call(
elements,
(acc, n) => acc + +n.value,
0
);
// или
let sum = 0;
for (const { value } of elements) {
sum += Number(value);
}
0
:const sum = (function sum(arr, i) {
return arr[i] ? parseFloat(arr[i].value) + sum(arr, i + 1) : 0;
})(elements, 0);
+
, в строку; строку отдаём в eval
; всё, сумма получена (ну, почти, если исходная коллекция элементов была пуста, то строка тоже будет пустой, так что надо не забыть подставить 0
вместо возможного undefined
, который является результатом выполнения пустой строки):const sum = eval(Array.from(elements, n => n.value).join('+')) ?? 0;
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 ]