Skip to content

Instantly share code, notes, and snippets.

@hmel1990
Created May 30, 2025 15:43
Show Gist options
  • Save hmel1990/1758146daa51a1a95a75733e0f2e07ae to your computer and use it in GitHub Desktop.
Save hmel1990/1758146daa51a1a95a75733e0f2e07ae to your computer and use it in GitHub Desktop.
callbacksDZ
<!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