2018-10-10 22:03:03 +00:00
---
id: 56bbb991ad1ed5201cd392cf
2021-02-06 04:42:36 +00:00
title: Write Reusable JavaScript with Functions
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-04-29 10:29:13 +00:00
videoUrl: 'https://scrimba.com/c/cL6dqfy'
forumTopicId: 18378
2021-01-13 02:31:00 +00:00
dashedName: write-reusable-javascript-with-functions
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2021-02-06 04:42:36 +00:00
In JavaScript, we can divide up our code into reusable parts called < dfn > functions< / dfn > .
2020-12-16 07:37:30 +00:00
2021-02-06 04:42:36 +00:00
Here's an example of a function:
2020-04-29 10:29:13 +00:00
```js
function functionName() {
console.log("Hello World");
}
```
2021-02-06 04:42:36 +00:00
You can call or < dfn > invoke</ dfn > this function by using its name followed by parentheses, like this: `functionName();` Each time the function is called it will print out the message `"Hello World"` on the dev console. All of the code between the curly braces will be executed every time the function is called.
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --instructions--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
< ol > < li > Create a function called < code > reusableFunction< / code > which prints < code > "Hi World"< / code > to the dev console.< / li > < li > Call the function.< / li > < / ol >
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`reusableFunction` should be a function.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(typeof reusableFunction === 'function');
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
`reusableFunction` should output "Hi World" to the dev console.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(hiWorldWasLogged);
2018-10-10 22:03:03 +00:00
```
2021-02-06 04:42:36 +00:00
You should call `reusableFunction` after you define it.
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
assert(/^\s*reusableFunction\(\)\s*/m.test(code));
2018-10-10 22:03:03 +00:00
```
2021-01-13 02:31:00 +00:00
# --seed--
## --before-user-code--
```js
var logOutput = "";
var originalConsole = console;
var nativeLog = console.log;
var hiWorldWasLogged = false;
function capture() {
console.log = function (message) {
if(message === 'Hi World') 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);
}
};
}
function uncapture() {
console.log = nativeLog;
}
capture();
```
## --after-user-code--
```js
uncapture();
if (typeof reusableFunction !== "function") {
(function() { return "reusableFunction is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
}
```
## --seed-contents--
```js
```
2020-12-16 07:37:30 +00:00
# --solutions--
2020-04-29 10:29:13 +00:00
2021-01-13 02:31:00 +00:00
```js
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();
```