Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.md
2023-01-13 09:48:11 -08:00

102 lines
2.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: 56bbb991ad1ed5201cd392cf
title: Написання багаторазового JavaScript із функціями
challengeType: 1
videoUrl: 'https://scrimba.com/c/cL6dqfy'
forumTopicId: 18378
dashedName: write-reusable-javascript-with-functions
---
# --description--
У JavaScript можна розділити код на багаторазові частини, які називаються <dfn>функціями</dfn>.
Приклад функції:
```js
function functionName() {
console.log("Hello World");
}
```
Ви можете викликати або <dfn>активувати</dfn> цю функцію, використавши дужки для написання її назви, ось так: `functionName();`. При кожному виклику функції на консоль буде виводитись повідомлення `Hello World`. При кожному виклику функції буде виконуватись код у фігурних дужках.
# --instructions--
<ol>
<li>
Створіть функцію під назвою <code>reusableFunction</code>, яка друкує рядок <code>Hi World</code> на консоль.
</li>
<li>
Викличте функцію.
</li>
</ol>
# --hints--
`reusableFunction` повинна бути функцією.
```js
assert(typeof reusableFunction === 'function');
```
Якщо `reusableFunction` буде викликано, то вона повинна виводити рядок `Hi World` на консоль.
```js
assert(testConsole());
```
Ви повинні викликати `reusableFunction` після її визначення.
```js
const functionStr = reusableFunction && __helpers.removeWhiteSpace(reusableFunction.toString());
const codeWithoutFunction = __helpers.removeWhiteSpace(code).replace(/reusableFunction\(\)\{/g, '');
assert(/reusableFunction\(\)/.test(codeWithoutFunction));
```
# --seed--
## --after-user-code--
```js
function testConsole() {
var logOutput = "";
var originalConsole = console;
var nativeLog = console.log;
var hiWorldWasLogged = false;
console.log = function (message) {
if(message === 'Hi World') {
console.warn(message)
hiWorldWasLogged = true;
}
if(message && message.trim) logOutput = message.trim();
if(nativeLog.apply) {
nativeLog.apply(originalConsole, arguments);
} else {
var nativeMsg = Array.prototype.slice.apply(arguments).join(' ');
nativeLog(nativeMsg);
}
};
reusableFunction();
console.log = nativeLog;
return hiWorldWasLogged;
}
```
## --seed-contents--
```js
```
# --solutions--
```js
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();
```