2019-06-28 16:04:59 +08:00

1.8 KiB

id, title, challengeType
id title challengeType
587d7b8c367417b2b2512b56 Use export to Reuse a Code Block 1

Description

Imagine you have a function, capitalizeFirstLetter, that simply takes in a string and returns the string with the first letter capitalized. You want to use this function in several different javascript files. In order to share the function with the files, you need to first export it.
export const capitalizeFirstLetter = (string) => {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

The above is a common way to export a single function, but you can achieve the same thing like this:

const capitalizeFirstLetter = (string) => {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

export { capitalizeFirstLetter };

After you export a function like this, you can import it in another file to use without having to rewrite the function.

Instructions

Create and export a variable named

Tests

tests:
  - text: <code>foo</code> is exported.
    testString: getUserInput => assert(getUserInput('index').match(/export\s+const\s+foo\s*=\s*"bar"/g), '<code>foo</code> is exported.');
  - text: <code>bar</code> is exported.
    testString: getUserInput => assert(getUserInput('index').match(/export\s+const\s+bar\s*=\s*"foo"/g), '<code>bar</code> is exported.');

Challenge Seed

"use strict";

Before Test

self.exports = function(){};

Solution

"use strict";
export const foo = "bar";
export const bar = "foo";