2018-09-30 23:01:58 +01:00
---
id: 56bbb991ad1ed5201cd392cf
title: Write Reusable JavaScript with Functions
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cL6dqfy'
2019-07-31 11:32:23 -07:00
forumTopicId: 18378
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
In JavaScript, we can divide up our code into reusable parts called < dfn > functions< / dfn > .
Here's an example of a function:
2019-05-17 06:20:30 -07:00
```js
function functionName() {
console.log("Hello World");
}
```
2018-09-30 23:01:58 +01:00
You can call or < dfn > invoke< / dfn > this function by using its name followed by parentheses, like this:
< code > functionName();< / code >
Each time the function is called it will print out the message < code > "Hello World"< / code > on the dev console. All of the code between the curly braces will be executed every time the function is called.
< / section >
## Instructions
< section id = 'instructions' >
< 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 >
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-27 02:57:38 -08:00
- text: < code > reusableFunction</ code > should be a function.
2019-07-13 00:07:53 -07:00
testString: assert(typeof reusableFunction === 'function');
2019-11-27 02:57:38 -08:00
- text: < code > reusableFunction</ code > should output "Hi World" to the dev console.
2019-07-13 00:07:53 -07:00
testString: assert(hiWorldWasLogged);
2019-11-27 02:57:38 -08:00
- text: You should call < code > reusableFunction</ code > after you define it.
2019-07-13 00:07:53 -07:00
testString: assert(/^\s*reusableFunction\(\)\s*/m.test(code));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
```
< / div >
### Before Test
< div id = 'js-setup' >
```js
var logOutput = "";
2019-03-21 16:20:23 +10:00
var originalConsole = console;
var nativeLog = console.log;
var hiWorldWasLogged = false;
2018-09-30 23:01:58 +01:00
function capture() {
console.log = function (message) {
2019-03-21 16:20:23 +10:00
if(message === 'Hi World') hiWorldWasLogged = true;
2018-09-30 23:01:58 +01: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-03-21 16:20:23 +10:00
console.log = nativeLog;
2018-09-30 23:01:58 +01:00
}
capture();
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
uncapture();
if (typeof reusableFunction !== "function") {
(function() { return "reusableFunction is not defined"; })();
} else {
(function() { return logOutput || "console.log never called"; })();
}
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();
```
< / section >