fix(curriculum): removed global variable ab(use) in lunch picker (#58014)

Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com>
This commit is contained in:
Prabhakar Yadav
2025-01-13 23:35:39 +05:30
committed by GitHub
parent dfa099d971
commit 157da269a0
@@ -15,25 +15,36 @@ In this lab, you'll build a program that helps in managing lunch options. You'll
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."`
2. You should create a function `addLunchToEnd` that takes an array as the first argument and a string as the second argument. The function should:
- Add the string to the end of the array.
- Log the string `"[Lunch Item] added to the end of the lunch menu."` to the console, where `[Lunch Item]` is the string passed to the function.
- Return the updated array.
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."`
3. You should create a function `addLunchToStart` that takes an array as the first argument and a string as the second argument. The function should:
- Add the string to the start of the array.
- Log the string `"[Lunch Item] added to the start of the lunch menu."` to the console, where `[Lunch Item]` is the string passed to the function.
- Return the updated array.
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."`
4. You should create a function `removeLastLunch` that takes an array as its argument. The function should:
- Remove the last element from the array.
- If the removal is successful, log the string `"[Lunch Item] removed from the end of the lunch menu."` to the console, where `[Lunch Item]` is the element removed from the array.
- If the array is empty, log the string `"No lunches to remove."` to the console.
- Return the updated array.
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."`
5. You should create a function `removeFirstLunch` that takes an array as its argument. The function should:
- Remove the first element from the array.
- If the removal is successful, log the string `"[Lunch Item] removed from the start of the lunch menu."` to the console, where `[Lunch Item]` is the element removed from the array.
- If the array is empty, log the string `"No lunches to remove."` to the console.
- Return the updated array.
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."`
6. You should create a function `getRandomLunch` that takes an array as its argument. The function should:
- Select a random element from the array.
- If successful, log the string `"Randomly selected lunch: [Lunch Item]"` to the console, where `[Lunch Item]` is a random element in the array.
- If the array is empty, log the string `"No lunches available."` to the console.
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."`
7. You should create a function `showLunchMenu` that takes an array as its argument and:
- If there are elements in the array, logs the string `"Menu items: [Lunch Item], [Lunch Item]...` to the console, where each `[Lunch item]` is one of the elements in the array, in order.
- If the array is empty, logs the string `"The menu is empty."` to the console.
# --hints--
@@ -49,20 +60,40 @@ You should define a function `addLunchToEnd`.
assert.isFunction(addLunchToEnd);
```
The function `addLunchToEnd` should take a single argument.
The function `addLunchToEnd` should have two parameters.
```js
assert.lengthOf(addLunchToEnd, 1);
assert.lengthOf(addLunchToEnd, 2);
```
`addLunchToEnd("Tacos")` should return the string `"Tacos added to the end of the lunch menu."`.
`addLunchToEnd(lunches, "Tacos")` should log the string `"Tacos added to the end of the lunch menu."` to the console.
```js
assert.strictEqual(addLunchToEnd("Tacos"), "Tacos added to the end of the lunch menu.");
const tempArr = [];
const temp = console.log;
const testLunches = []
try {
console.log = obj => tempArr.push(obj);
addLunchToEnd(testLunches, "Tacos");
assert.strictEqual(tempArr[0], "Tacos added to the end of the lunch menu.");
addLunchToEnd(testLunches, "Pizza");
assert.strictEqual(tempArr[1], "Pizza added to the end of the lunch menu.");
} finally {
console.log = temp;
}
```
// 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.");
`addLunchToEnd(["Pizza", "Tacos"], "Burger")` should return `["Pizza", "Tacos", "Burger"]`.
```js
const temp = console.log;
console.log = () => {}
try {
assert.deepStrictEqual(addLunchToEnd(["Pizza", "Tacos"], "Burger"), ["Pizza", "Tacos", "Burger"]);
assert.deepStrictEqual(addLunchToEnd(["Fries"], "Tacos"), ["Fries", "Tacos"]);
} finally {
console.log = temp;
}
```
You should define a function `addLunchToStart`.
@@ -71,20 +102,40 @@ You should define a function `addLunchToStart`.
assert.isFunction(addLunchToStart);
```
The function `addLunchToStart` should take a single argument.
The function `addLunchToStart` should have two parameters.
```js
assert.lengthOf(addLunchToStart, 1);
assert.lengthOf(addLunchToStart, 2);
```
`addLunchToStart("Sushi")` should return the string `"Sushi added to the start of the lunch menu."`.
`addLunchToStart(lunches, "Sushi")` should log the string `"Sushi added to the start of the lunch menu."` to the console.
```js
assert.strictEqual(addLunchToStart("Sushi"), "Sushi added to the start of the lunch menu.");
const tempArr = [];
const testLunches = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
addLunchToStart(testLunches, "Sushi");
assert.strictEqual(tempArr[0], "Sushi added to the start of the lunch menu.");
addLunchToStart(testLunches, "Burger");
assert.strictEqual(tempArr[1], "Burger added to the start of the lunch menu.");
} finally {
console.log = temp;
}
```
// 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.");
`addLunchToStart(["Burger", "Sushi"], "Pizza")` should return `["Pizza", "Burger", "Sushi"]`.
```js
const temp = console.log;
console.log = () => {}
try {
assert.deepStrictEqual(addLunchToStart(["Burger", "Sushi"], "Pizza"), ["Pizza", "Burger", "Sushi"]);
assert.deepStrictEqual(addLunchToStart(["Noodles"], "Sushi"), ["Sushi", "Noodles"]);
} finally {
console.log = temp;
}
```
You should define a function `removeLastLunch`.
@@ -93,40 +144,130 @@ You should define a function `removeLastLunch`.
assert.isFunction(removeLastLunch)
```
When the `lunches` array is empty, the function `removeLastLunch` should return the string `"No lunches to remove."`.
The function `removeLastLunch` should have one parameter.
```js
lunches = [];
assert.strictEqual(removeLastLunch(), "No lunches to remove.");
assert.lengthOf(removeLastLunch, 1);
```
When `lunches = ["Stew", "Soup", "Toast"]`, the function `removeLastLunch` should return the string `"Toast removed from the end of the lunch menu."`.
When the input array is empty, the function `removeLastLunch` should log the string `"No lunches to remove."` to the console.
```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.");
const testLunches = [];
const tempArr = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
removeLastLunch(testLunches);
assert.strictEqual(tempArr[0], "No lunches to remove.");
} finally {
console.log = temp;
}
```
When the `lunches` array is empty, the function `removeFirstLunch` should return the string `"No lunches to remove."`.
`removeLastLunch(["Stew", "Soup", "Toast"])` should log the string `"Toast removed from the end of the lunch menu."` to the console.
```js
lunches = [];
assert.strictEqual(removeFirstLunch(), "No lunches to remove.");
let testLunches = ["Stew", "Soup", "Toast"];
const tempArr = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
removeLastLunch(testLunches);
assert.strictEqual(tempArr[0], "Toast removed from the end of the lunch menu.");
testLunches = ["Rice", "Pizza"];
removeLastLunch(testLunches);
assert.strictEqual(tempArr[1], "Pizza removed from the end of the lunch menu.");
} finally {
console.log = temp;
}
```
When `lunches = ["Salad", "Eggs", "Cheese"]`, the function `removeFirstLunch` should return the string `"Salad removed from the start of the lunch menu."`.
`removeLastLunch(["Sushi", "Pizza", "Noodles"])` should return `["Sushi", "Pizza"]`.
```js
lunches = ["Salad", "Eggs", "Cheese"];
assert.strictEqual(removeFirstLunch(), "Salad removed from the start of the lunch menu.");
const temp = console.log;
console.log = () => {}
try {
assert.deepStrictEqual(removeLastLunch(["Sushi", "Pizza", "Noodles"]), ["Sushi", "Pizza"]);
assert.deepStrictEqual(removeLastLunch(["Pizza", "Burger"]), ["Pizza"]);
} finally {
console.log = temp;
}
```
// 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 `removeFirstLunch`.
```js
assert.isFunction(removeFirstLunch);
```
The function `removeFirstLunch` should have a single parameter.
```js
assert.lengthOf(removeFirstLunch, 1);
```
When the input array is empty, the function `removeFirstLunch` should log the string `"No lunches to remove."` to the console.
```js
const testLunches = [];
const tempArr = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
removeFirstLunch(testLunches);
assert.strictEqual(tempArr[0], "No lunches to remove.");
} finally {
console.log = temp;
}
```
`removeFirstLunch(["Salad", "Eggs", "Cheese"])` should log the string `"Salad removed from the start of the lunch menu."` to the console.
```js
let testLunches = ["Salad", "Eggs", "Cheese"];
const tempArr = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
removeFirstLunch(testLunches);
assert.strictEqual(tempArr[0], "Salad removed from the start of the lunch menu.");
testLunches = ["Stew", "Soup", "Toast"]
removeFirstLunch(testLunches);
assert.strictEqual(tempArr[1], "Stew removed from the start of the lunch menu.");
} finally {
console.log = temp;
}
```
`removeFirstLunch(["Sushi", "Pizza", "Burger"])` should return `["Pizza", "Burger"]`.
```js
const temp = console.log;
console.log = () => {}
try {
assert.deepStrictEqual(removeFirstLunch(["Sushi", "Pizza", "Burger"]), ["Pizza", "Burger"]);
assert.deepStrictEqual(removeFirstLunch(["Pizza", "Burger"]), ["Burger"]);
} finally {
console.log = temp;
}
```
`addLunchToEnd`, `addLunchToStart`, `removeLastLunch`, and `removeFirstLunch` should return the same array passed as an argument after updating it.
```js
const temp = console.log;
console.log = () => {}
const testLunches = ["Spam"];
try {
assert.equal(addLunchToEnd(testLunches, "Pizza"), testLunches);
assert.equal(addLunchToStart(testLunches, "Caviar"), testLunches);
assert.equal(removeLastLunch(testLunches), testLunches);
assert.equal(removeFirstLunch(testLunches), testLunches);
} finally {
console.log = temp;
}
```
You should define a function `getRandomLunch`.
@@ -135,34 +276,58 @@ You should define a function `getRandomLunch`.
assert.isFunction(getRandomLunch);
```
When the `lunches` array is empty, the function `getRandomLunch` should return the string `"No lunches available."`.
The function `getRandomLunch` should have a single parameter.
```js
lunches = [];
assert.strictEqual(getRandomLunch(), "No lunches available.");
assert.lengthOf(getRandomLunch, 1);
```
When `lunches = ["Fish", "Fries", "Roast"]`, the function `getRandomLunch` should return a string in the format `"Randomly selected lunch: [Lunch Item]"`.
When the input array is empty, the function `getRandomLunch` should log the string `"No lunches available."` to the console.
```js
const temp = Math.random;
const testLunches = [];
const tempArr = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
getRandomLunch(testLunches);
assert.strictEqual(tempArr[0], "No lunches available.");
} finally {
console.log = temp;
}
```
lunches = ["Fish", "Fries", "Roast"];
When the input array is not empty, the function `getRandomLunch` should log a string in the format `"Randomly selected lunch: [Lunch Item]"` to the console.
// check that it correctly outputs the first item
Math.random = () => 0;
assert.equal(getRandomLunch(), `Randomly selected lunch: ${lunches[0]}`);
```js
const testLunches = ["Fish", "Fries", "Roast"];
const tempRandom = Math.random;
const tempArr = [];
const temp = console.log;
// second item
Math.random = () => 0.4;
assert.equal(getRandomLunch(), `Randomly selected lunch: ${lunches[1]}`);
try {
console.log = obj => tempArr.push(obj);
// third item
Math.random = () => 0.8;
assert.equal(getRandomLunch(), `Randomly selected lunch: ${lunches[2]}`);
// check that it correctly outputs the first item
Math.random = () => 0;
getRandomLunch(testLunches);
assert.strictEqual(tempArr[0], `Randomly selected lunch: ${testLunches[0]}`);
// restore Math.random
Math.random = temp;
// second item
Math.random = () => 0.4;
getRandomLunch(testLunches);
assert.strictEqual(tempArr[1], `Randomly selected lunch: ${testLunches[1]}`);
// third item
Math.random = () => 0.8;
getRandomLunch(testLunches);
assert.strictEqual(tempArr[2], `Randomly selected lunch: ${testLunches[2]}`);
} finally {
// restore Math.random
Math.random = tempRandom;
console.log = temp;
}
```
You should define a function `showLunchMenu`.
@@ -171,21 +336,43 @@ You should define a function `showLunchMenu`.
assert.isFunction(showLunchMenu);
```
When the `lunches` array is empty, the function `showLunchMenu` should return the string `"The menu is empty."`.
The function `showLunchMenu` should have a single parameter.
```js
lunches = [];
assert.strictEqual(showLunchMenu(), "The menu is empty.");
assert.lengthOf(showLunchMenu, 1);
```
When `lunches = ["Greens", "Corns", "Beans"]`, the function `showLunchMenu` should return a string in the format `"Menu items: Greens, Corns, Beans"`.
When the input array is empty, the function `showLunchMenu` should log the string `"The menu is empty."` to the console.
```js
lunches = ["Greens", "Corns", "Beans"];
assert.strictEqual(showLunchMenu(), "Menu items: Greens, Corns, Beans");
const testLunches = [];
const tempArr = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
showLunchMenu(testLunches);
assert.strictEqual(tempArr[0], "The menu is empty.");
} finally {
console.log = temp;
}
```
lunches = ["Pizza", "Burger", "Fries"];
assert.strictEqual(showLunchMenu(), "Menu items: Pizza, Burger, Fries");
`showLunchMenu(["Greens", "Corns", "Beans"])` should log `"Menu items: Greens, Corns, Beans"` to the console.
```js
let testLunches = ["Greens", "Corns", "Beans"];
const tempArr = [];
const temp = console.log;
try {
console.log = obj => tempArr.push(obj);
showLunchMenu(testLunches);
assert.strictEqual(tempArr[0], "Menu items: Greens, Corns, Beans");
testLunches = ["Pizza", "Burger", "Fries"]
showLunchMenu(testLunches);
assert.strictEqual(tempArr[1], "Menu items: Pizza, Burger, Fries");
} finally {
console.log = temp;
}
```
# --seed--
@@ -201,61 +388,69 @@ assert.strictEqual(showLunchMenu(), "Menu items: Pizza, Burger, Fries");
```js
const lunches = [];
// Add a new lunch to the list (at the end)
function addLunchToEnd(newLunch) {
// Add a new lunch to the end of the list
function addLunchToEnd(lunches, newLunch) {
lunches.push(newLunch);
return `${newLunch} added to the end of the lunch menu.`;
console.log(`${newLunch} added to the end of the lunch menu.`);
return lunches;
}
// Add a new lunch to the start of the list
function addLunchToStart(newLunch) {
function addLunchToStart(lunches, newLunch) {
lunches.unshift(newLunch);
return `${newLunch} added to the start of the lunch menu.`;
console.log(`${newLunch} added to the start of the lunch menu.`);
return lunches;
}
// Remove the last lunch from the list
function removeLastLunch() {
function removeLastLunch(lunches) {
const removedLunch = lunches.pop();
if (removedLunch) {
return `${removedLunch} removed from the end of the lunch menu.`;
console.log(`${removedLunch} removed from the end of the lunch menu.`);
} else {
return "No lunches to remove.";
console.log("No lunches to remove.");
}
return lunches;
}
// Remove the first lunch from the list
function removeFirstLunch() {
function removeFirstLunch(lunches) {
const removedLunch = lunches.shift();
if (removedLunch) {
return `${removedLunch} removed from the start of the lunch menu.`;
console.log(`${removedLunch} removed from the start of the lunch menu.`);
} else {
return "No lunches to remove.";
console.log("No lunches to remove.");
}
return lunches;
}
// Function to get a random lunch from the list
function getRandomLunch() {
function getRandomLunch(lunches) {
if (lunches.length === 0) {
return "No lunches available.";
console.log("No lunches available.");
}
else {
const randomIndex = Math.floor(Math.random() * lunches.length);
const randomLunch = lunches[randomIndex];
console.log(`Randomly selected lunch: ${randomLunch}`);
}
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() {
// Function to display all the lunches in the list
function showLunchMenu(lunches) {
if (lunches.length === 0) {
return "The menu is empty.";
console.log("The menu is empty.");
}
else {
console.log(`Menu items: ${lunches.join(', ')}`);
}
return `Menu items: ${lunches.join(', ')}`;
}
addLunchToEnd("Tacos");
addLunchToStart("Sushi");
removeLastLunch();
removeFirstLunch();
getRandomLunch();
showLunchMenu();
addLunchToEnd(lunches, "Tacos");
addLunchToStart(lunches, "Sushi");
removeLastLunch(lunches);
removeFirstLunch(lunches);
getRandomLunch(lunches);
showLunchMenu(lunches);
```