|
| 1 | +--- |
| 2 | +id: 66b205e6eacba4c4e54ea434 |
| 3 | +title: Build an Email Masker |
| 4 | +challengeType: 14 |
| 5 | +dashedName: build-an-email-masker |
| 6 | +--- |
| 7 | + |
| 8 | +# --description-- |
| 9 | + |
| 10 | +In this lab, you will mask the username part of an email address with asterisks. Masking is a term used to hide or replace sensitive information with asterisks or other characters. |
| 11 | + |
| 12 | +For example, if the email address was `[email protected]`, then the masked email address will be `m*******[email protected]`. |
| 13 | + |
| 14 | +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. |
| 15 | + |
| 16 | +**User Stories:** |
| 17 | + |
| 18 | +1. Create a function named `maskEmail` that takes `email` as an argument. |
| 19 | +2. Inside the function, you should mask the `email` and append the domain name to it. Remember that you can use methods like `slice`, `repeat`, `indexOf` `slice` or even `replace` to help you. |
| 20 | +3. Outside the function, declare a variable named `email` to store the email address you want to mask. |
| 21 | +4. Call the `maskEmail` function with the `email` variable and output the result to the console. |
| 22 | +5. `maskEmail("[email protected]")` should return `"a*******[email protected]"`. |
| 23 | +6. `maskEmail("[email protected]")` should return `"f**********[email protected]"`. |
| 24 | + |
| 25 | +# --hints-- |
| 26 | + |
| 27 | +You should define a funtion named `maskEmail`. |
| 28 | + |
| 29 | +```js |
| 30 | +assert.isFunction(maskEmail); |
| 31 | +``` |
| 32 | + |
| 33 | +The `maskEmail` funtion should take a string, `email` as an argument. |
| 34 | + |
| 35 | +```js |
| 36 | +assert.match(maskEmail.toString(), /\s*maskEmail\(\s*\w+\s*\)/); |
| 37 | +``` |
| 38 | + |
| 39 | +Outside the function, you should have an `email` variable. |
| 40 | + |
| 41 | +```js |
| 42 | +assert.isDefined(email); |
| 43 | +``` |
| 44 | + |
| 45 | +You should assign a valid email address to your `email` variable. |
| 46 | + |
| 47 | +```js |
| 48 | +assert.match(email, /^[\w\.-]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}$/); |
| 49 | +``` |
| 50 | + |
| 51 | +`maskEmail("[email protected]")` should return `"a*******[email protected]"`. |
| 52 | + |
| 53 | +```js |
| 54 | +assert. strictEqual( maskEmail( "[email protected]"), "a*******[email protected]"); |
| 55 | +``` |
| 56 | + |
| 57 | +`maskEmail("[email protected]")` should return `"f**********[email protected]"`. |
| 58 | + |
| 59 | +```js |
| 60 | +assert. strictEqual( maskEmail( "[email protected]"), "f**********[email protected]"); |
| 61 | +``` |
| 62 | + |
| 63 | +Your `maskEmail` should produce the correct result. |
| 64 | + |
| 65 | +```js |
| 66 | +assert. strictEqual( maskEmail( "[email protected]"), "j***********[email protected]"); |
| 67 | +assert. strictEqual( maskEmail( "[email protected]"), "j******[email protected]"); |
| 68 | +assert. strictEqual( maskEmail( "[email protected]"), "j********[email protected]"); |
| 69 | +assert. strictEqual( maskEmail( "[email protected]"), "m************[email protected]"); |
| 70 | +``` |
| 71 | + |
| 72 | +You should log the function call to `maskEmail` to the console. |
| 73 | + |
| 74 | +```js |
| 75 | +assert.match(__helpers.removeJSComments(code), /console\.log\(maskEmail\(email\)\)/); |
| 76 | +``` |
| 77 | + |
| 78 | +# --seed-- |
| 79 | + |
| 80 | +## --seed-contents-- |
| 81 | + |
| 82 | +```js |
| 83 | + |
| 84 | +``` |
| 85 | + |
| 86 | +# --solutions-- |
| 87 | + |
| 88 | +```js |
| 89 | +function maskEmail(email) { |
| 90 | + const atIndex = email.indexOf("@"); |
| 91 | + const userName = email.slice(0, atIndex); |
| 92 | + const domain = email.slice(atIndex); |
| 93 | + |
| 94 | + const maskedName = |
| 95 | + userName[0] + "*".repeat(userName.length - 2) + userName[userName.length - 1] + domain; |
| 96 | + |
| 97 | + return maskedName; |
| 98 | +} |
| 99 | + |
| 100 | +const email = "[email protected]"; |
| 101 | +console.log(maskEmail(email)); |
| 102 | +``` |
0 commit comments