/** * Dotais ir masīvs. Nepieciešams sakārtot nepāra skaitļus pēc vērtības pieaugošā secībā, * bet pāra skaitļus atstāt savās vietās * / export function oddSort(numbers) { // jūsu kods šeit } console.log(oddSort([2, 3, 7, 4, 6, 1, 5, 8, 9])); // [2, 1, 3, 4, 6, 5, 7, 8, 9] console.log(oddSort([2, 4, 6, 8])); // [2, 4, 6, 8] console.log(oddSort([3, 7, 1, 5, 9])); // [1, 3, 5, 7, 9] // Nepieciešams pārbaudīt uzdevuma risinājumu divās pakalpojumos, izsaucot: // 1. checkResult(url1, solution) // 2. checkResult(url2, solution) // // checkResult: (url: string, solution: string | number) => Promise<boolean>; // // - Ja abi pieprasījumi atgriež true, izvadīt success // - Ja vismaz viens atgriež false, izvadīt fail // - Ja vismaz viens neatbild, izvadīt error // - Ja vismaz viens atbild ilgāk par 1 sekundi, izvadīt timeout import { checkResult } from '...'; export async function checkResults() { const solution = 'Any answer'; const url1 = 'yandex.ru'; const url2 = 'google.com'; checkResult(url1, solution); checkResult(url2, solution); }
sobes.tech AI
Atbilde no AI
javascript export function oddSort(numbers) { // Извлекаем нечетные числа const oddNumbers = numbers.filter(n => n % 2 !== 0).sort((a, b) => a - b); let oddIndex = 0; // Формируем новый массив, заменяя нечетные числа отсортированными return numbers.map(n => { if (n % 2 !== 0) { return oddNumbers[oddIndex++]; } return n; }); }
// Асинхронная функция для проверки результатов import { checkResult } from '...';
export async function checkResults() { const solution = 'Any answer'; const url1 = 'yandex.ru'; const url2 = 'google.com';
// Запускаем оба запроса с таймаутом function withTimeout(promise, ms) { return Promise.race([ promise, new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), ms)) ]); }
try { const [res1, res2] = await Promise.all([ withTimeout(checkResult(url1, solution), 1000), withTimeout(checkResult(url2, solution), 1000) ]);
if (res1 === true && res2 === true) {
console.log('success');
} else if (res1 === false || res2 === false) {
console.log('fail');
} else {
console.log('error');
}
} catch (e) { if (e.message === 'timeout') { console.log('timeout'); } else { console.log('error'); } } }
// Пример вызова console.log(oddSort([2, 3, 7, 4, 6, 1, 5, 8, 9])); // [2, 1, 3, 4, 6, 5, 7, 8, 9]