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

1.7 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 three 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 };

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";
const foo = "bar";
const bar = "foo";

Before Test

self.exports = function(){};

Solution

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