/ ** * Given an array. Sort the odd numbers in ascending order, * leave the even numbers in their places * / export function oddSort(numbers) { // your code here } 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] // Need to test the solution with two services by calling: // 1. checkResult(url1, solution) // 2. checkResult(url2, solution) // // checkResult: (url: string, solution: string | number) => Promise<boolean>; // // - If both requests return true - log 'success' // - If at least one returns false - log 'fail' // - If at least one does not respond - log 'error' // - If at least one responds longer than 1 sec - log '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
Answer from AI
javascript export function oddSort(numbers) { // Extract odd numbers const oddNumbers = numbers.filter(n => n % 2 !== 0).sort((a, b) => a - b); let oddIndex = 0; // Create a new array, replacing odd numbers with sorted ones return numbers.map(n => { if (n % 2 !== 0) { return oddNumbers[oddIndex++]; } return n; }); }
// Asynchronous function to check results import { checkResult } from '...';
export async function checkResults() { const solution = 'Any answer'; const url1 = 'yandex.ru'; const url2 = 'google.com';
// Run both requests with timeout 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'); } } }
// Example call console.log(oddSort([2, 3, 7, 4, 6, 1, 5, 8, 9])); // [2, 1, 3, 4, 6, 5, 7, 8, 9]