mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-30 12:05:39 -05:00
feat(curriculum): add lunch picker lab (#56054)
Co-authored-by: Jessica Wilkins <67210629+jdwilkin4@users.noreply.github.com> Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com> Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com> Co-authored-by: Tom <20648924+moT01@users.noreply.github.com>
This commit is contained in:
@@ -2166,6 +2166,12 @@
|
||||
"You will review how to add and remove elements from an array using methods like <code>push</code>, <code>pop</code>, <code>shift</code>, and <code>unshift</code>."
|
||||
]
|
||||
},
|
||||
"lab-lunch-picker-program": {
|
||||
"title": "Build a Lunch Picker Program",
|
||||
"intro": [
|
||||
"In this lab, you will review working with arrays and random numbers by building a lunch picker program."
|
||||
]
|
||||
},
|
||||
"mokm": { "title": "161", "intro": [] },
|
||||
"froz": { "title": "162", "intro": [] },
|
||||
"quiz-javascript-arrays": {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build a Lunch Picker Program
|
||||
block: lab-lunch-picker-program
|
||||
superBlock: front-end-development
|
||||
---
|
||||
|
||||
## Introduction to the Build a Lunch Picker Program
|
||||
|
||||
In this lab, you create a lunch picker user arrays and random numbers
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "Build a Lunch Picker Program",
|
||||
"isUpcomingChange": true,
|
||||
"usesMultifileEditor": true,
|
||||
"dashedName": "lab-lunch-picker-program",
|
||||
"order": 160,
|
||||
"superBlock": "front-end-development",
|
||||
"challengeOrder": [{ "id": "66db529d37ad966480ebb633", "title": "Build a Lunch Picker Program" }],
|
||||
"helpCategory": "JavaScript",
|
||||
"blockType": "lab"
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
---
|
||||
id: 66db529d37ad966480ebb633
|
||||
title: Build a Lunch Picker Program
|
||||
challengeType: 14
|
||||
dashedName: build-a-lunch-picker-program
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab, you'll build a program that helps in managing lunch options. You'll work with an array of lunches, add and remove items from the array, and randomly select a lunch option.
|
||||
|
||||
**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should create a `lunches` variable and assign it an empty array that will be used to store lunch items.
|
||||
|
||||
2. You should create a function `addLunchToEnd` that takes a string parameter and adds it to the end of the `lunches` array and returns a string in the format: `"[Lunch Item] added to the end of the lunch menu."`
|
||||
|
||||
3. You should create a function `addLunchToStart` that takes a string parameter and adds it to the start of the `lunches` array and returns a string in the format: `"[Lunch Item] added to the start of the lunch menu."`
|
||||
|
||||
4. You should create a function `removeLastLunch` that removes the last lunch item from the `lunches` array and:
|
||||
- If successful, returns a string in the format: `"[Lunch Item] removed from the end of the lunch menu."`
|
||||
- If the `lunches` array is empty, returns a string: `"No lunches to remove."`
|
||||
|
||||
5. You should create a function `removeFirstLunch` that removes the first lunch item from the `lunches` array and:
|
||||
- If successful, returns a string in the format: `"[Lunch Item] removed from the start of the lunch menu."`
|
||||
- If the `lunches` array is empty, returns a string: `"No lunches to remove."`
|
||||
|
||||
6. You should create a function `getRandomLunch` that selects a random lunch item from the `lunches` array and:
|
||||
- If successful, returns a string in the format: `"Randomly selected lunch: [Lunch Item]"`
|
||||
- If the `lunches` array is empty, returns a string: `"No lunches available."`
|
||||
|
||||
7. You should create a function `showLunchMenu` that:
|
||||
- If there are items in the `lunches` array, returns a string in the format: `"Menu items: [Lunch Item], [Lunch Item]...`
|
||||
- If the `lunches` array is empty, returns a string: `"The menu is empty."`
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a variable `lunches` and assign it an empty array to store the lunch items.
|
||||
|
||||
```js
|
||||
assert.isArray(lunches);
|
||||
```
|
||||
|
||||
You should define a function `addLunchToEnd`.
|
||||
|
||||
```js
|
||||
assert.isFunction(addLunchToEnd);
|
||||
```
|
||||
|
||||
The function `addLunchToEnd` should take a single argument.
|
||||
|
||||
```js
|
||||
assert.lengthOf(addLunchToEnd, 1);
|
||||
```
|
||||
|
||||
`addLunchToEnd("Tacos")` should return the string `"Tacos added to the end of the lunch menu."`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(addLunchToEnd("Tacos"), "Tacos added to the end of the lunch menu.");
|
||||
|
||||
// prevent hardcoding
|
||||
assert.strictEqual(addLunchToEnd("Pizza"), "Pizza added to the end of the lunch menu.");
|
||||
assert.strictEqual(addLunchToEnd("Burger"), "Burger added to the end of the lunch menu.");
|
||||
```
|
||||
|
||||
You should define a function `addLunchToStart`.
|
||||
|
||||
```js
|
||||
assert.isFunction(addLunchToStart);
|
||||
```
|
||||
|
||||
The function `addLunchToStart` should take a single argument.
|
||||
|
||||
```js
|
||||
assert.lengthOf(addLunchToStart, 1);
|
||||
```
|
||||
|
||||
`addLunchToStart("Sushi")` should return the string `"Sushi added to the start of the lunch menu."`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(addLunchToStart("Sushi"), "Sushi added to the start of the lunch menu.");
|
||||
|
||||
// prevent hardcoding
|
||||
assert.strictEqual(addLunchToStart("Salad"), "Salad added to the start of the lunch menu.");
|
||||
assert.strictEqual(addLunchToStart("Pasta"), "Pasta added to the start of the lunch menu.");
|
||||
```
|
||||
|
||||
You should define a function `removeLastLunch`.
|
||||
|
||||
```js
|
||||
assert.isFunction(removeLastLunch)
|
||||
```
|
||||
|
||||
When the `lunches` array is empty, the function `removeLastLunch` should return the string `"No lunches to remove."`.
|
||||
|
||||
```js
|
||||
lunches = [];
|
||||
assert.strictEqual(removeLastLunch(), "No lunches to remove.");
|
||||
```
|
||||
|
||||
When `lunches = ["Stew", "Soup", "Toast"]`, the function `removeLastLunch` should return the string `"Toast removed from the end of the lunch menu."`.
|
||||
|
||||
```js
|
||||
lunches = ["Stew", "Soup", "Toast"];
|
||||
assert.strictEqual(removeLastLunch(), "Toast removed from the end of the lunch menu.");
|
||||
|
||||
// prevent hardcoding
|
||||
assert.strictEqual(removeLastLunch(), "Soup removed from the end of the lunch menu.");
|
||||
assert.strictEqual(removeLastLunch(), "Stew removed from the end of the lunch menu.");
|
||||
```
|
||||
|
||||
When the `lunches` array is empty, the function `removeFirstLunch` should return the string `"No lunches to remove."`.
|
||||
|
||||
```js
|
||||
lunches = [];
|
||||
assert.strictEqual(removeFirstLunch(), "No lunches to remove.");
|
||||
```
|
||||
|
||||
When `lunches = ["Salad", "Eggs", "Cheese"]`, the function `removeFirstLunch` should return the string `"Salad removed from the start of the lunch menu."`.
|
||||
|
||||
```js
|
||||
lunches = ["Salad", "Eggs", "Cheese"];
|
||||
assert.strictEqual(removeFirstLunch(), "Salad removed from the start of the lunch menu.");
|
||||
|
||||
// prevent hardcoding
|
||||
assert.strictEqual(removeFirstLunch(), "Eggs removed from the start of the lunch menu.");
|
||||
assert.strictEqual(removeFirstLunch(), "Cheese removed from the start of the lunch menu.");
|
||||
```
|
||||
|
||||
You should define a function `getRandomLunch`.
|
||||
|
||||
```js
|
||||
assert.isFunction(getRandomLunch);
|
||||
```
|
||||
|
||||
When the `lunches` array is empty, the function `getRandomLunch` should return the string `"No lunches available."`.
|
||||
|
||||
```js
|
||||
lunches = [];
|
||||
assert.strictEqual(getRandomLunch(), "No lunches available.");
|
||||
```
|
||||
|
||||
When `lunches = ["Fish", "Fries", "Roast"]`, the function `getRandomLunch` should return a string in the format `"Randomly selected lunch: [Lunch Item]"`.
|
||||
|
||||
```js
|
||||
const temp = Math.random;
|
||||
|
||||
lunches = ["Fish", "Fries", "Roast"];
|
||||
|
||||
// check that it correctly outputs the first item
|
||||
Math.random = () => 0;
|
||||
assert.equal(getRandomLunch(), `Randomly selected lunch: ${lunches[0]}`);
|
||||
|
||||
// second item
|
||||
Math.random = () => 0.4;
|
||||
assert.equal(getRandomLunch(), `Randomly selected lunch: ${lunches[1]}`);
|
||||
|
||||
// third item
|
||||
Math.random = () => 0.8;
|
||||
assert.equal(getRandomLunch(), `Randomly selected lunch: ${lunches[2]}`);
|
||||
|
||||
// restore Math.random
|
||||
Math.random = temp;
|
||||
```
|
||||
|
||||
You should define a function `showLunchMenu`.
|
||||
|
||||
```js
|
||||
assert.isFunction(showLunchMenu);
|
||||
```
|
||||
|
||||
When the `lunches` array is empty, the function `showLunchMenu` should return the string `"The menu is empty."`.
|
||||
|
||||
```js
|
||||
lunches = [];
|
||||
assert.strictEqual(showLunchMenu(), "The menu is empty.");
|
||||
```
|
||||
|
||||
When `lunches = ["Greens", "Corns", "Beans"]`, the function `showLunchMenu` should return a string in the format `"Menu items: Greens, Corns, Beans"`.
|
||||
|
||||
```js
|
||||
lunches = ["Greens", "Corns", "Beans"];
|
||||
assert.strictEqual(showLunchMenu(), "Menu items: Greens, Corns, Beans");
|
||||
|
||||
lunches = ["Pizza", "Burger", "Fries"];
|
||||
assert.strictEqual(showLunchMenu(), "Menu items: Pizza, Burger, Fries");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
const lunches = [];
|
||||
|
||||
// Add a new lunch to the list (at the end)
|
||||
function addLunchToEnd(newLunch) {
|
||||
lunches.push(newLunch);
|
||||
return `${newLunch} added to the end of the lunch menu.`;
|
||||
}
|
||||
|
||||
// Add a new lunch to the start of the list
|
||||
function addLunchToStart(newLunch) {
|
||||
lunches.unshift(newLunch);
|
||||
return `${newLunch} added to the start of the lunch menu.`;
|
||||
}
|
||||
|
||||
// Remove the last lunch from the list
|
||||
function removeLastLunch() {
|
||||
const removedLunch = lunches.pop();
|
||||
if (removedLunch) {
|
||||
return `${removedLunch} removed from the end of the lunch menu.`;
|
||||
} else {
|
||||
return "No lunches to remove.";
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the first lunch from the list
|
||||
function removeFirstLunch() {
|
||||
const removedLunch = lunches.shift();
|
||||
if (removedLunch) {
|
||||
return `${removedLunch} removed from the start of the lunch menu.`;
|
||||
} else {
|
||||
return "No lunches to remove.";
|
||||
}
|
||||
}
|
||||
|
||||
// Function to get a random lunch from the list
|
||||
function getRandomLunch() {
|
||||
if (lunches.length === 0) {
|
||||
return "No lunches available.";
|
||||
}
|
||||
const randomIndex = Math.floor(Math.random() * lunches.length);
|
||||
const randomLunch = lunches[randomIndex];
|
||||
return `Randomly selected lunch: ${randomLunch}`;
|
||||
}
|
||||
|
||||
// New function to display the lunches as a numbered menu using a for loop
|
||||
function showLunchMenu() {
|
||||
if (lunches.length === 0) {
|
||||
return "The menu is empty.";
|
||||
}
|
||||
return `Menu items: ${lunches.join(', ')}`;
|
||||
}
|
||||
|
||||
addLunchToEnd("Tacos");
|
||||
addLunchToStart("Sushi");
|
||||
removeLastLunch();
|
||||
removeFirstLunch();
|
||||
getRandomLunch();
|
||||
showLunchMenu();
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user