2018-10-10 18:03:03 -04:00
---
id: 56bbb991ad1ed5201cd392cf
title: Write Reusable JavaScript with Functions
challengeType: 1
2019-08-28 16:26:13 +03:00
videoUrl: https://scrimba.com/c/cL6dqfy
forumTopicId: 18378
2018-10-10 18:03:03 -04:00
localeTitle: Запись многоразового JavaScript с функциями
---
## Description
2019-08-28 16:26:13 +03:00
< section id = 'description' >
В JavaScript мы можем разделить наш код на многократно используемые < dfn > функции,< / dfn > называемые < dfn > функциями< / dfn > . Вот пример функции: < blockquote > function functionName () { < br > console.log («Hello World»); < br > } < / blockquote > Вы можете вызвать или < dfn > вызвать< / dfn > эту функцию, используя е е имя, за которым следуют скобки, например: < code > functionName();< / code > Каждый раз, когда функция вызывается, она выводит сообщение < code > " Hello World" < / code > на консоль dev. Весь код между фигурными фигурными скобками будет выполняться каждый раз, когда вызывается функция.
< / section >
2018-10-10 18:03:03 -04:00
## Instructions
2019-08-28 16:26:13 +03:00
< section id = 'instructions' >
< ol > < li > Создайте функцию, называемую < code > reusableFunction< / code > которая печатает < code > " Hi World" < / code > в dev-консоли. < / li > < li > Вызовите функцию. < / li > < / ol >
< / section >
2018-10-10 18:03:03 -04:00
## Tests
< section id = 'tests' >
```yml
tests:
2019-08-28 16:26:13 +03:00
- text: < code > reusableFunction</ code > should be a function
testString: assert(typeof reusableFunction === 'function');
- text: < code > reusableFunction</ code > should output "Hi World" to the dev console
testString: assert(hiWorldWasLogged);
- text: Call < code > reusableFunction</ code > after you define it
testString: assert(/^\s*reusableFunction\(\)\s*/m.test(code));
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Example
function ourReusableFunction() {
console.log("Heyya, World");
}
ourReusableFunction();
// Only change code below this line
```
< / div >
2019-08-28 16:26:13 +03:00
### Before Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-setup' >
```js
var logOutput = "";
2019-08-28 16:26:13 +03:00
var originalConsole = console;
var nativeLog = console.log;
var hiWorldWasLogged = false;
2018-10-10 18:03:03 -04:00
function capture() {
console.log = function (message) {
2019-08-28 16:26:13 +03:00
if(message === 'Hi World') hiWorldWasLogged = true;
2018-10-10 18:03:03 -04:00
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);
}
};
}
function uncapture() {
2019-08-28 16:26:13 +03:00
console.log = nativeLog;
2018-10-10 18:03:03 -04:00
}
capture();
```
< / div >
2019-08-28 16:26:13 +03:00
### After Tests
2018-10-10 18:03:03 -04:00
< div id = 'js-teardown' >
```js
2019-08-28 16:26:13 +03:00
uncapture();
if (typeof reusableFunction !== "function") {
(function() { return "reusableFunction is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
}
2018-10-10 18:03:03 -04:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
2019-08-28 16:26:13 +03:00
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();
2018-10-10 18:03:03 -04:00
```
2019-08-28 16:26:13 +03:00
2018-10-10 18:03:03 -04:00
< / section >