freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../es6/use-export-to-share-a-code-...

84 lines
2.1 KiB
Markdown
Raw Normal View History

2019-05-31 20:21:47 +00:00
---
id: 587d7b8c367417b2b2512b56
title: Use export to Share a Code Block
challengeType: 1
---
## Description
<section id='description'>
2019-06-01 00:48:23 +00:00
Imagine a file called <code>math_functions.js</code>, it contains several functions related to mathematical operations. One of them is stored in a variable, <code>add</code>, that takes in two numbers and returns the sum of them. You want to use this function in several different JavaScript files. In order to share it with the files, you need to first <code>export</code> it.
2019-05-31 20:21:47 +00:00
```js
export const add = (x, y) => {
return x + y;
}
```
2019-06-01 00:48:23 +00:00
The above is a common way to export a single function, but you can achieve the same thing like this:
2019-05-31 20:21:47 +00:00
```js
const add = (x, y) => {
return x + y;
}
export { add };
```
2019-06-01 00:48:23 +00:00
After you export a variable or function, you can import it in another file to use without having to rewrite the code. You can export multiple things by repeating the first example for each thing you want to export, or by placing them all in the export statement of the second example like this:
2019-05-31 20:21:47 +00:00
```js
export { add, subtract };
```
</section>
## Instructions
<section id='instructions'>
There are two functions related to strings in the editor. Export both of them using the method of your choice.
</section>
## Tests
<section id='tests'>
```yml
tests:
2019-06-01 00:48:23 +00:00
- text: You should properly export <code>uppercaseString</code>.
testString: assert(code.match(/(export\s+const\s+uppercaseString|export\s*{\s*(uppercaseString[^}]*|[^,]*,\s*uppercaseString\s*)})/g));
- text: You should properly export <code>lowercaseString</code>.
testString: assert(code.match(/(export\s+const\s+lowercaseString|export\s*{\s*(lowercaseString[^}]*|[^,]*,\s*lowercaseString\s*)})/g));
2019-05-31 20:21:47 +00:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
2019-06-01 00:48:23 +00:00
const uppercaseString = (string) => {
2019-05-31 20:21:47 +00:00
return string.toUpperCase();
}
const lowercaseString = (string) => {
return string.toLowerCase()
}
```
</div>
</section>
## Solution
<section id='solution'>
```js
2019-06-01 00:48:23 +00:00
export const uppercaseString = (string) => {
2019-05-31 20:21:47 +00:00
return string.toUpperCase();
}
export const lowercaseString = (string) => {
return string.toLowerCase()
}
```
2019-06-01 00:48:23 +00:00
2019-05-31 20:21:47 +00:00
</section>