Skip to content

Commit 29c570d

Browse files
committed
Ch 9 Studio Solution
1 parent 203c578 commit 29c570d

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

arrays/test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let num = "234.54"
2+
num.splice(".")
3+
console.log(num.length)

loops/studio/solution.js

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,43 @@ const input = require('readline-sync');
22

33
// Part A: #1 Populate these arrays
44

5-
let protein = [];
6-
let grains = [];
7-
let veggies = [];
8-
let beverages = [];
9-
let desserts = [];
5+
let protein = ['chicken', 'pork', 'tofu', 'beef', 'fish', 'beans']
6+
let grains = ['rice', 'pasta', 'corn', 'potato', 'quinoa', 'crackers']
7+
let veggies = ['peas', 'green beans', 'kale', 'edamame', 'broccoli', 'asparagus']
8+
let beverages = ['juice', 'milk', 'water', 'soy milk', 'soda', 'tea']
9+
let desserts = ['apple', 'banana', 'more kale', 'ice cream', 'chocolate', 'kiwi']
1010

1111

1212
function mealAssembly(protein, grains, veggies, beverages, desserts, numMeals) {
1313
let pantry = [protein, grains, veggies, beverages, desserts];
1414
let meals = [];
15-
15+
1616
/// Part A #2: Write a ``for`` loop inside this function
1717
/// Code your solution for part A #2 below this comment (and above the return statement) ... ///
18+
for (i = 0; i < numMeals; i++) {
19+
let individualMeal = [];
1820

19-
21+
for(let j = 0; j < pantry.length; j++) {
22+
individualMeal.push(pantry[j][i])
23+
}
24+
meals.push(individualMeal)
2025
return meals;
2126
}
2227

2328

2429
function askForNumber() {
25-
numMeals = input.question("How many meals would you like to make?");
26-
30+
let numPass = false
31+
2732
/// CODE YOUR SOLUTION TO PART B here ///
2833

34+
while(!numPass) {
35+
numMeals = input.question("How many meals would you like to make?");
36+
if (numMeals > 6 || numMeals < 1 || isNan (numMeals)) {
37+
console.log("Value out of range. Please try again.")
38+
} else {
39+
numPass = true;
40+
}
41+
}
2942
return numMeals;
3043
}
3144

@@ -35,34 +48,36 @@ function generatePassword(string1, string2) {
3548

3649
/// Code your Bonus Mission Solution here ///
3750

51+
for (let i = 0; i<string1.length; i++) {
52+
code += string1[i] + string2[i]
3853
return code;
3954
}
4055

4156
function runProgram() {
42-
57+
4358
/// TEST PART A #2 HERE ///
4459
/// UNCOMMENT the two lines of code below that invoke the mealAssembly function (starting with 'let meals =') and print the result ///
4560
/// Change the final input variable (aka numMeals) here to ensure your solution makes the right number of meals ///
4661
/// We've started with the number 2 for now. Does your solution still work if you change this value? ///
47-
48-
// let meals = mealAssembly(protein, grains, veggies, beverages, desserts, 2);
49-
// console.log(meals)
50-
5162

63+
let meals = mealAssembly(protein, grains, veggies, beverages, desserts, 2);
64+
console.log(meals)
65+
66+
}
5267
/// TEST PART B HERE ///
5368
/// UNCOMMENT the next two lines to test your ``askForNumber`` solution ///
5469
/// Tip - don't test this part until you're happy with your solution to part A #2 ///
55-
56-
// let mealsForX = mealAssembly(protein, grains, veggies, beverages, desserts, askForNumber());
57-
// console.log(mealsForX);
70+
71+
let mealsForX = mealAssembly(protein, grains, veggies, beverages, desserts, askForNumber());
72+
console.log(mealsForX);
5873

5974
/// TEST PART C HERE ///
6075
/// UNCOMMENT the remaining commented lines and change the password1 and password2 strings to ensure your code is doing its job ///
6176

62-
// let password1 = '';
63-
// let password2 = '';
64-
// console.log("Time to run the password generator so we can update the menu tomorrow.")
65-
// console.log(`The new password is: ${generatePassword(password1, password2)}`);
77+
let password1 = '6789';
78+
let password2 = 'dgef';
79+
console.log("Time to run the password generator so we can update the menu tomorrow.")
80+
console.log(`The new password is: ${generatePassword(password1, password2)}`);
6681
}
6782

6883
module.exports = {
@@ -74,5 +89,6 @@ module.exports = {
7489
mealAssembly: mealAssembly,
7590
askForNumber: askForNumber,
7691
generatePassword: generatePassword,
77-
runProgram: runProgram
78-
};
92+
runProgram: runProgram
93+
}
94+
}

0 commit comments

Comments
 (0)