Created
May 30, 2025 15:43
-
-
Save hmel1990/1758146daa51a1a95a75733e0f2e07ae to your computer and use it in GitHub Desktop.
callbacksDZ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script> | |
// Задание 1: | |
function calculate(a, b, onSuccess, onError) | |
{ | |
if (b === 0) | |
{ | |
onError("Деление на ноль запрещено"); | |
} | |
else | |
{ | |
const result = a / b; | |
onSuccess(result); | |
} | |
} | |
function onSuccess(result) | |
{ | |
console.log("Результат:", result); | |
} | |
function onError(error) { | |
console.error("Ошибка:", error); | |
} | |
calculate(10, 2, onSuccess, onError); // 5 | |
calculate(10, 0, onSuccess, onError); // Ошибка: Деление на ноль запрещено | |
// ========================================================================================== | |
// Задание 2 | |
let array = [1, 2, 3, 4, 5]; | |
function processArray(array, callback) | |
{ | |
return array.map(callback); | |
} | |
function logElement(element) | |
{ | |
console.log(element); | |
return element; // чтобы map вернул исходные значения | |
} | |
function squareElement(element) | |
{ | |
return element * element; | |
} | |
// Примеры использования: | |
processArray(array, logElement); // 1 2 3 4 5 | |
console.log(processArray(array, squareElement)); // [1, 4, 9, 16, 25] | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment