mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-19 04:00:56 -04:00
feat(curriculum): add functions lab (#56330)
Co-authored-by: jdwilkin4 <jwilkin4@hotmail.com> Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
259b7c31e1
commit
c56d2cbd62
@@ -2838,7 +2838,10 @@
|
||||
"In these lecture videos, you will learn about modules, imports, and exports in JavaScript."
|
||||
]
|
||||
},
|
||||
"lnmg": { "title": "179", "intro": [] },
|
||||
"lab-password-generator": {
|
||||
"title": "Build a Password Generator App",
|
||||
"intro": ["In this lab, you'll build a password generator app."]
|
||||
},
|
||||
"review-javascript-fundamentals": {
|
||||
"title": "JavaScript Fundamentals Review",
|
||||
"intro": [
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build a Password Generator App
|
||||
block: lab-password-generator
|
||||
superBlock: full-stack-developer
|
||||
---
|
||||
|
||||
## Introduction to the Build a Password Generator App
|
||||
|
||||
In this lab, you'll build a password generator app.
|
||||
11
curriculum/challenges/_meta/lab-password-generator/meta.json
Normal file
11
curriculum/challenges/_meta/lab-password-generator/meta.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "Build a Password Generator App",
|
||||
"isUpcomingChange": true,
|
||||
"usesMultifileEditor": true,
|
||||
"blockType": "lab",
|
||||
"dashedName": "lab-password-generator",
|
||||
"superBlock": "full-stack-developer",
|
||||
"challengeOrder": [{ "id": "66f53dc2c5bd6a11d6c3282f", "title": "Build a Password Generator App" }],
|
||||
"helpCategory": "JavaScript",
|
||||
"blockLayout": "link"
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
---
|
||||
id: 66f53dc2c5bd6a11d6c3282f
|
||||
title: Build a Password Generator App
|
||||
challengeType: 25
|
||||
dashedName: lab-password-generator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab, you'll practice using functions by building a random password generator.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should create a function called `generatePassword` that takes a parameter. You can name the parameter whatever you like.
|
||||
2. Your function should return a string which represents a randomly generated password. You should use the following string and different `Math` methods to help you return a new string with random characters in it: `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()`.
|
||||
3. You should define a variable called `password` and assign it the result of calling the `generatePassword` function with a numeric argument that represents the desired password length.
|
||||
4. You should have a `console.log` that logs the message `"Generated password:"` followed by the `password` variable.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `generatePassword` function with a parameter. You can name the parameter whatever you like.
|
||||
|
||||
```js
|
||||
assert.isFunction(generatePassword);
|
||||
assert.lengthOf(generatePassword, 1);
|
||||
```
|
||||
|
||||
Your `generatePassword` function should return a string.
|
||||
|
||||
```js
|
||||
const result = generatePassword(5);
|
||||
assert.isString(result);
|
||||
```
|
||||
|
||||
Your `generatePassword` function should return a new string that is the correct length.
|
||||
|
||||
```js
|
||||
const length = 8;
|
||||
const password = generatePassword(length);
|
||||
assert.lengthOf(password, length);
|
||||
```
|
||||
|
||||
Your function should return a randomly generated password with valid characters.
|
||||
|
||||
```js
|
||||
const validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
|
||||
const password = generatePassword(10);
|
||||
for (let char of password) {
|
||||
assert.include(validChars, char);
|
||||
}
|
||||
```
|
||||
|
||||
Your function should return a new random string each time it is called.
|
||||
|
||||
```js
|
||||
const password1 = generatePassword(10);
|
||||
const password2 = generatePassword(10);
|
||||
assert.notStrictEqual(password1, password2);
|
||||
```
|
||||
|
||||
You should have a `password` variable.
|
||||
|
||||
```js
|
||||
assert.isDefined(password);
|
||||
```
|
||||
|
||||
Your `password` variable should be a string.
|
||||
|
||||
```js
|
||||
assert.isString(password);
|
||||
```
|
||||
|
||||
You should call the `generatePassword` function with a numeric argument and store the returned password in the `password` variable.
|
||||
|
||||
```js
|
||||
assert.isDefined(password);
|
||||
assert.isString(password);
|
||||
const testLength = password.length;
|
||||
assert.strictEqual(password.length, testLength);
|
||||
assert.match(password, /^[A-Za-z0-9!@#$%^&*()]+$/);
|
||||
```
|
||||
|
||||
You should log the generated password to the console.
|
||||
|
||||
```js
|
||||
const condition1 = /console\.log\(\s*["']Generated\s+password:\s*["']\s*\+\s*password\s*\);?/gm.test(code);
|
||||
const condition2 = /console\.log\(\s*`Generated\s+password:\s*\$\{password\}`\s*\);?/gm.test(code);
|
||||
assert.isTrue(condition1 || condition2);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const generatePassword = (length) => {
|
||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()";
|
||||
let password = "";
|
||||
for (let i = 0; i < length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * chars.length);
|
||||
password += chars[randomIndex];
|
||||
}
|
||||
return password;
|
||||
};
|
||||
|
||||
const password = generatePassword(12);
|
||||
console.log(`Generated password: ${password}`);
|
||||
```
|
||||
@@ -376,6 +376,7 @@
|
||||
{
|
||||
"dashedName": "lecture-understanding-modules-imports-and-exports"
|
||||
},
|
||||
{ "dashedName": "lab-password-generator" },
|
||||
{ "dashedName": "review-javascript-fundamentals" },
|
||||
{ "dashedName": "quiz-javascript-fundamentals" }
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user