Files
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/write-reusable-javascript-with-functions.md
Randell Dawson e18c6955b0 fix(curriculum): Improve test to allow any valid function call for Write Reusable JavaScript with Functions challenge (#41538)
* fix: improve test to allow function call anywhere

* fix: force user to put console.log in function
2021-03-20 12:23:32 +00:00

95 lines
2.2 KiB
Markdown

---
id: 56bbb991ad1ed5201cd392cf
title: Write Reusable JavaScript with Functions
challengeType: 1
videoUrl: 'https://scrimba.com/c/cL6dqfy'
forumTopicId: 18378
dashedName: write-reusable-javascript-with-functions
---
# --description--
In JavaScript, we can divide up our code into reusable parts called <dfn>functions</dfn>.
Here's an example of a function:
```js
function functionName() {
console.log("Hello World");
}
```
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.
# --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>
# --hints--
`reusableFunction` should be a function.
```js
assert(typeof reusableFunction === 'function');
```
If `reusableFunction` is called, it should output the string `Hi World` to the console.
```js
assert(testConsole());
```
You should call `reusableFunction` once it is defined.
```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();
```