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
2021-01-13 03:31:00 +01:00
dashedName: write-reusable-javascript-with-functions
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2018-09-30 23:01:58 +01:00
In JavaScript, we can divide up our code into reusable parts called < dfn > functions< / dfn > .
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
Here's an example of a function:
2019-05-17 06:20:30 -07:00
```js
function functionName() {
console.log("Hello World");
}
```
2021-03-02 16:12:12 -08: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.
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2021-04-16 19:33:58 +01:00
< ol >
< li >
Create a function called < code > reusableFunction< / code > which prints the string < code > Hi World< / code > to the dev console.
< / li >
< li >
Call the function.
< / li >
< / ol >
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`reusableFunction` should be a function.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof reusableFunction === 'function');
2018-09-30 23:01:58 +01:00
```
2021-03-20 06:23:32 -06:00
If `reusableFunction` is called, it should output the string `Hi World` to the console.
2018-09-30 23:01:58 +01:00
```js
2021-03-20 06:23:32 -06:00
assert(testConsole());
2020-11-27 19:02:05 +01:00
```
2018-09-30 23:01:58 +01:00
2021-03-20 06:23:32 -06:00
You should call `reusableFunction` once it is defined.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
2021-03-20 06:23:32 -06:00
const functionStr = reusableFunction & & __helpers.removeWhiteSpace(reusableFunction.toString());
const codeWithoutFunction = __helpers.removeWhiteSpace(code).replace(/reusableFunction\(\)\{/g, '');
assert(/reusableFunction\(\)/.test(codeWithoutFunction));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2021-03-20 06:23:32 -06:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2021-03-20 06:23:32 -06:00
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();
2019-03-21 16:20:23 +10:00
console.log = nativeLog;
2021-03-20 06:23:32 -06:00
return hiWorldWasLogged;
2018-09-30 23:01:58 +01:00
}
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
2021-03-04 04:14:28 +07:00
2020-11-27 19:02:05 +01:00
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function reusableFunction() {
console.log("Hi World");
}
reusableFunction();
```